In the lab,we will create the Lambda function for Alexa’s backend.
Make sure you’ve selected the right region N.Virginia before you start.
Why we have to choose N.Virginia ?
1. N.Virginia for English (US) or English (CA) skills
2. EU (Ireland) region for English (UK), English (IN), German or French (FR) skills
3. US West (Oregon) for Japanese and English (AU) skills.
As we choose our Alexa Skill’s language as English (US) , we only need to use N.Virginia
If you are familiar with Serverless Framework, you can go direct to Option 2: Deploy via Serverless Framework section. However, it is recommended to follow the manual deployment procedure for the first time.
Go to IAM Console, choose Roles on the left side bar
click Create Role
Under Choose the service that will use this role, choose Lambda
Click Next: Permissions, Next: Tags, Next: Review
In the Review page, enter alexa-lambda-role
for the Role name, and choose Create role
Click the alexa-lambda-role
, under Permissions tab, click Add inline policy
In Create Policy page, select JSON, and copy & paste the following policy. Please remember to
replace <device-table-name>
. Check your DynamoDB table name in DynamoDB
Console. If you followed Build Device Binding UI,
a DynamoDB table has already been created. This policy grant the Lambda to read items from device table and put logs
to CloudWatch.
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
"*"
],
"Effect": "Allow"
},
{
"Action": [
"dynamodb:Query"
],
"Resource": [
"arn:aws:dynamodb:*:*:table/<device-table-name>/index/ByUsernameThingName"
],
"Effect": "Allow"
},
{
"Action": [
"iot:UpdateThingShadow"
],
"Resource": [
"*"
],
"Effect": "Allow"
}
]
}
Click Review Policy
In the Name field, input a name for this policy
Click Create Policy
Go to Lambda Console, click Create function
Select Author from scratch, and enter the following information:
Click Create Function. Your function should be created and you will move to Configuration
In the Configuration designer, under Add triggers select the Alexa Smart Home trigger
In the Configure triggers section, add the Skill ID from the developer console in the box specified.
Leave Enable trigger checked. This enables the Amazon Alexa service to call your Lambda function.
Click Add and then click Save
Firstly, you should git clone this repo, go to root folder in your Cloud9 environment, and type in terminal
git clone https://github.com/aws-samples/aws-alexa-workshop-smarthome
cd aws-alexa-workshop-smarthome
Edit config.json
file, you can find these information in AWS Console and Alexa
Console
Run npm install --production
to install dependencies
Make a zip file to include index.js
, auth.js
, config.json
, alexa/
and node_modules/
,
these files/directories should be located at the root level of the zip file
zip -q -r lambda.zip index.js auth.js config.json alexa/ node_modules/
Download the zip file to local
Go to AWS Lambda Console, click the lambda function
Under Function code, click Upload to upload the zip file
Click the Save button at the top right corner
Skip this part if you have completed the previous parts in this article. The only difference is the resources are provisioned automatically.
All of the above settings in this section including Lambda execution role, permission, code,
Alexa Smart Home SKill can be configured using Serverless Framework.
However, you only need to edit config.json
file and deploy via sls
command.
Edit config.json
file
Run npm install
to install dependencies including development dependencies
Run sls deploy
After success deployment, you should be able to see a Lambda named
alexa-smarthome-{stage}-backend
.