7. Create the Alexa backend Lambda

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.

Create Lambda Execution Role

  1. Go to IAM Console, choose Roles on the left side bar

  2. click Create Role

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

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

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

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

  7. 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"
        }
    ]
    }
    
  8. Click Review Policy

  9. In the Name field, input a name for this policy

  10. Click Create Policy

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

  7. Click Add and then click Save

Upload Code

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

    zip -q -r lambda.zip index.js auth.js config.json alexa/ node_modules/
    

  5. Download the zip file to local

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

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

  8. Click the Save button at the top right corner

Deploy via Serverless Framework

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.

  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.