Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Latest commit

 

History

History

java-lambda-smartapp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

java-lambda-smartapp java-logo aws-logo

This sample demonstrates a simple Java server using AWS Lambdas.

Requirements

Installing the Example

Follow the steps below to install and run this sample SmartApp.

  1. Install the AWS CLI.

  2. Configure the CLI to use your account.

    1. Get an access key to use when deploying and configuring your SmartApp.

      1. Find "My Security Credentials" under your account settings.
      2. Expand "Access keys (access key ID and secret access key)".
      3. If you already have an access key and you know the secret for it, you can use that one.
      4. Otherwise, create a new key and note the id and secret.
    2. Run aws configure and populate as follows:

    Option Value
    AWS Access Key ID id from above
    AWS Secret Access Key secret from above
    Default region name choose an AWS region geographically close to your end-user's location
    Default output format enter 'json' here
  3. Create a role for the lambda to use. You'll find "Roles" under "My Security Credentials". Create the role with the following options:

    Option Value
    Service Lambda
    Policy AWSLambdaFullAccess
    Tags None
    Role Name Something like "LambdaFullAccess"
  4. To build the zip file, un the following command from the root project directory:

    ./gradlew examples:java-lambda-smartapp:buildZip
    
  5. Deploy the zip file by running the following command. Be sure to update your account id and the version.

    aws lambda create-function --function-name smartapp-example \
        --zip-file fileb://build/distributions/java-lambda-smartapp-<version>.zip \
        --role arn:aws:iam::<account id>:role/lambda_full_access \
        --handler app.App \
        --runtime java8 \
        --timeout 15 \
        --memory-size 256
    

    Later, when you want to deploy an updated zip file, use aws lambda update-function-code instead:

    aws lambda update-function-code --function-name smartapp-example \
        --zip-file fileb://build/distributions/java-lambda-smartapp-<version>.zip
    

    (If you need to update configuration later, use the aws lambda update-function-configuration.)

  6. Add permissions for SmartThings access to Lambda. This gives SmartThings (which has a principle id of 906037444270) permission to call the Lambda.

    aws lambda add-permission \
        --function-name <my-function-name> \
        --statement-id smartthings \
        --principal 906037444270 \
        --action lambda:InvokeFunction
    

Create SmartApp

First, you'll need to determine the target ARN for the function you just deployed. Find the function you just deployed in the AWS Console and you will find the ARN in the upper right corner.

Then, follow these steps using the developer workspace:

  1. Create a new project and choose "Automation for the SmartThings App".
  2. Give your project a unique name.
  3. Choose "Automation Connector | SmartApp" under "Develop" in the left-hand menu.
  4. Choose "AWS Lambda" and enter the target ARN you found above.
  5. Select the r:devices:* and x:devices:* scopes and click "Next".
  6. Give your application a name and hit "Save".
  7. Save the new project and click the "Deploy to Test" button.

Install SmartApp

First, be sure to enable developer mode in your SmartThings application to see the self-published automation.

Now you should see your SmartApp listed (near the bottom) when you add a new Automation.

There are lots of sample log messages in this example. You should see the results of this in the AWS console. Look for "View logs in CloudWatch" under the "Monitoring" tab of the function.

References

More about SmartThings

If you are not familiar with SmartThings, we have extensive on-line documentation.

To create and manage your services and devices on SmartThings, create an account in the developer workspace.

The SmartThings Community is a good place share and ask questions.

There is also a SmartThings reddit community where you can read and share information.

License and Copyright

Licensed under the Apache License, Version 2.0

Copyright 2019 SmartThings, Inc.