This sample demonstrates a simple Java server using AWS Lambdas.
- Java 1.8
- SmartThings developer account
- Amazon Web Services account
Follow the steps below to install and run this sample SmartApp.
-
Install the AWS CLI.
-
Configure the CLI to use your account.
-
Get an access key to use when deploying and configuring your SmartApp.
- Find "My Security Credentials" under your account settings.
- Expand "Access keys (access key ID and secret access key)".
- If you already have an access key and you know the secret for it, you can use that one.
- Otherwise, create a new key and note the id and secret.
-
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 -
-
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" -
To build the zip file, un the following command from the root project directory:
./gradlew examples:java-lambda-smartapp:buildZip
-
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
.) -
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
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:
- Create a new project and choose "Automation for the SmartThings App".
- Give your project a unique name.
- Choose "Automation Connector | SmartApp" under "Develop" in the left-hand menu.
- Choose "AWS Lambda" and enter the target ARN you found above.
- Select the
r:devices:*
andx:devices:*
scopes and click "Next". - Give your application a name and hit "Save".
- Save the new project and click the "Deploy to Test" button.
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.
- SmartThings Developer Workspace
- Hosting with AWS Lambda
- AWS CLI
- AWS Lambda
- Lambda Functions in Java
- AWS Lambda Execution Role
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.
Licensed under the Apache License, Version 2.0
Copyright 2019 SmartThings, Inc.