Skip to content

Latest commit

 

History

History
120 lines (87 loc) · 4.08 KB

create-lambda.md

File metadata and controls

120 lines (87 loc) · 4.08 KB

Create the Alexa backend Lambda

Make sure you've selected the right region

  • N.Virginia for English (US) or English (CA) skills
  • EU (Ireland) region for English (UK), English (IN), German or French (FR) skills
  • US West (Oregon) for Japanese and English (AU) skills.

If you are familiar with Serverless Framework, you can go direct to Deploy via Serverless Framework section. However, it is recommended to follow the manual deployment procedure for the first time.

Create Lambda Execution Role

  1. Go to IAM Console, click Create Role

  2. Under Choose the service that will use this role, choose Lambda

  3. Click Next: Permissions, Next: Tags, Next: Review

  4. In the Review page, enter alexa-lambda-role for the Role name, and choose Create role

  5. Click the alexa-lambda-role, under Permissions tab, click Add inline policy

  6. 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 Create a Web Interface to bind Smart Lamp to User, 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"
        }
    ]
}

Create the Lambda function

  1. Go to Lambda Console, click Create function

  2. Select Author from scratch, and enter the following information:

    • Name: Provide a name for your Lambda function, should be same
    • Runtime: To use the sample code provided in this topic, choose Node.js 10.x
    • Role: select a role you previously created
  3. Click Create Function. Your function should be created and you will move to Configuration

  4. In the Configuration designer, under Add triggers select the Alexa Smart Home trigger

  5. In the Configure triggers section, add the Skill ID from the developer console in the box specified.

  6. Leave Enable trigger checked. This enables the Amazon Alexa service to call your Lambda function. If you don't enable it when you create, you will not be able to enable it in the console later

  7. Click Add and then click Save

Upload Code

  1. Change directory to src

  2. Edit config.json file, you can find these information in AWS Console and Alexa Console

  3. Run npm install --production to install dependencies

  4. 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

  5. Go to AWS Lambda Console, click the lambda function

  6. Under Function code, click Upload to upload the zip file

Deploy via Serverless Framework

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.

  1. Edit config.json file

  2. Run npm install to install dependencies including development dependencies

  3. Run sls deploy

After success deployment, you should be able to see a Lambda named alexa-smarthome-{stage}-backend.