Step 2: Implement Skill Code

In the previous step, you created a new smart home skill in the Alexa developer console. Now you implement your smart home skill code as an Amazon Web Services (AWS) Lambda function. This function consists of your skill code, configuration information, and an Identity and Access Management (IAM) role that allows Lambda to run the skill code on your behalf. Optionally, Lambda functions can store data in AWS DynamoDB and write logs to AWS CloudWatch.

You can write your code in the AWS Lambda console directly or in a development environment appropriate for the programming language that you plan to use to code your skill. You can author a Lambda function in Node.js, Java, Python, C#, Go, Ruby, or PowerShell. For simplicity, this tutorial step uses the AWS Lambda console code editor and provides code examples in Python and Node.js.

Substeps to implement skill code

To implement the skill code that controls a virtual light bulb, complete the following substeps:

Step a: Set up permissions

In this substep, you create a policy that defines permissions that allow write access for log entries to AWS CloudWatch. In Step b, you attach the policy to an IAM role for your Lambda function.

To create an IAM policy in the AWS developer console

  1. Sign in to the AWS management console.
  2. Under AWS Services > All services > Security, Identity, & Compliance, click IAM.
    Or, go directly to the IAM dashboard.
  3. On the IAM dashboard, from the left menu under Access management, click Policies.
  4. On the Policies page, click Create Policy.
  5. On the Create policy page, select the JSON tab.
  6. Copy and paste the following policy definition into the text area.

Copied to clipboard.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:CreateLogGroup",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}
  1. Click Next: Tags.
  2. Click Next: Review.
  3. Under the Create policy > Review policy, for Name, enter smart_home_skill_policy.
    You can leave the description blank.
  4. To create the policy, click Create policy.

Now, you can see the smart_home_skill_policy in the list of available policies.

Step b: Create an IAM role

In this substep, you attach the smart home policy to a new IAM role. In Step c, you assign the role to your Lambda function.

To create an IAM role for your AWS Lambda function in the AWS developer console

  1. On the IAM dashboard, from the left menu under Access management, click Roles.
  2. On the Roles page, click Create role.
  3. On the IAM > Roles > Create role page, enter the following information:
    1. Under Select trusted entity, for Trusted entity type, select AWS service.
    2. Under Common use cases, select Lambda.
    3. Click Next.
  4. On the Add permissions page, filter to find the smart_home_skill_policy policy that you just created.
  5. Select, click the check box next to smart_home_skill_policy policy,and then click Next.
  6. On the Name, review, and create page, for Role name, enter lambda_smart_home_skill_role, and then click Create role.

Now, you can see the lambda_smart_home_skill_role in the list of available roles.

Step c: Create a Lambda function

In this substep, you create a Lambda function and add the IAM role that allows Lambda to run your code on your behalf. Then, you add the example skill code.

To create an AWS Lambda function in the AWS developer console

  1. Navigate to the Lambda console.
  2. On the Lambda console, click Create function.
    If you've created Lambda functions, the console displays a list of your functions.
  3. Click Create function.
  4. Under Create function, select Author from scratch.
  5. Under Basic information, enter the following information:
    1. For Function name, enter my-smart-home-skill.
    2. For Runtime, choose Python 3.9 or Node.js 14.x.
    3. For Architecture, leave the default.
    4. For Permissions > Change default execution role, select Use an existing role.
    5. For Existing role, select lambda_smart_home_skill_role, and then select Create function.
  6. On the Lambda > Functions > my-smart-home-skill page, under Function overview, click + Add trigger.
  7. On the Add trigger page, enter the following information about to trigger your skill:
    1. For Trigger configuration, select Alexa Smart Home.
    2. For Application ID, paste the skill ID that you saved in Step 1: Create a Smart Home Skill.
    3. To add the trigger to invoke your skill, click Add.

Step d: Add skill code

Now, you add the example skill code to your Lambda function. On receipt of a directive from Alexa, the Lambda function executes your skill code.

Add the skill code to your Lambda function

  1. On the Lambda > Functions > my-smart-home-skill page, to view the source code, select the Code tab.
    For Python, your skill code resides in the file, lambda_function.py. For Node.js, your skill code resides in the file, index.js.
  2. To display the function code, click lambda_function.py or index.js.
  3. Replace the existing code with the following smart home skill code that controls the virtual light bulb.
    The code supports the AcceptGrant, Discovery, TurnOn, and TurnOff directives.

  1. To save the file, under File, select Save.
  2. To deploy the function, select Deploy.

In the green box at the top of the Lambda > Functions > my-smart-home-skill page, you see a message that says that the function updated successfully.

Step e: Test the Lambda function

After you save your skill code, you can invoke the Lambda function and view the results in the AWS developer console. The following code examples send an Alexa.Discovery request and an Alexa.PowerController request to turn on the light.

Define a discovery test

  1. On the Lambda > Functions > my-smart-home-skill page, select the Test tab.
  2. Under Test event, select New event.
  3. For Template, from the drop-down menu, select Alexa Smart Home Discovery.
  4. For Name, enter DiscoveryTest.
  5. In the code editor, replace the existing code with the following test code.

Copied to clipboard.

{
    "directive": {
        "header": {
            "namespace": "Alexa.Discovery",
            "name": "Discover",
            "payloadVersion": "3",
            "messageId": "1bd5d003-31b9-476f-ad03-71d471922820"
        },
        "payload": {
            "scope": {
                "type": "BearerToken",
                "token": "access-token-from-skill"
            }
        }
    }
}
  1. To save the test code, click Save changes.

Define a Power on test

  1. Under Test event, select New event.
  2. For Template, from the drop-down menu, select Alexa Smart Home Control Turn On Request.
  3. For Name, enter PowerOnTest.
  4. In the code editor, replace the existing code with the following test code.

Copied to clipboard.

{
  "directive": {
    "header": {
      "namespace": "Alexa.PowerController",
      "name": "TurnOn",
      "messageId": "1bd5d003-31b9-476f-ad03-71d471922820",
      "correlationToken": "1bd5d003-31b9-476f-ad03-71d471922820",
      "payloadVersion": "3"
    },
    "endpoint": {
      "scope": {
        "type": "BearerToken",
        "token": "access-token-from-skill"
      },
      "endpointId": "sample-light-01",
      "cookie": {}
    },
    "payload": {}
  }
}
  1. To save the test code, click Save changes.

To test your Lambda function on the AWS developer console

  1. To open the test, under Test event, select Saved event, and then from the drop-down menu, select DiscoveryTest.
  2. To run the test, under Test event, click Test.
    If the test is successful, you see Execution result: succeeded.
  3. To view the logs, in the Execution result box, expand Details.
  4. Repeat Steps 1–3 for PowerOnTest.

Step f: Copy the resource name

Every Lambda function has an Amazon Resource Name (ARN) that defines the endpoint to the function. Alexa accesses your skill code by the ARN.

The following image shows a sample ARN displayed in the Lambda console.

Formatted resource name for the smart home skill.

To copy the Lambda function ARN in the AWS developer console

  1. At the top of the Lambda > Functions > my-smart-home-skill page, select Copy ARN.
  2. In a convenient place, such as Notepad on Windows or TextEdit on Mac, paste the ARN.
    You use this value in Step 3: Configure the Service Endpoint.