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 in the AWS console

Complete the following substeps to implement the skill code that controls a virtual light bulb.

  1. Create an IAM role
  2. Create a Lambda function
  3. Add skill code
  4. Test the Lambda function
  5. Copy the resource name

Substep 2.1: Create an IAM role

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

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

  1. Sign in to the AWS management console.
  2. Open the IAM dashboard.
  3. On the IAM dashboard, from the left menu under Access management, click Roles.
  4. On the IAM > Roles page, click Create role.
  5. For IAM > Roles > Create role page, enter the following information:
    1. Under Select trusted entity, for Trusted entity type, select AWS service.
    2. Under Use cases, select Lambda, and then click Next.
  6. Under Add permissions, filter to find the policy called, AWSLambdaBasicExecutionRole.
  7. Select the check box next to the AWSLambdaBasicExecutionRole policy and AWS managed type, and then click Next.
  8. Under Name, review, and create page, for Role name, enter lambda_smart_home_skill_role, and then, at the bottom of the page, click Create role.

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

Substep 2.2: 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 dashboard. If you've created Lambda functions, the console displays a list of your functions.
  2. On the AWS Lambda page, click Create function.
  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 a version of Python or Node.js.
    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 to trigger your skill:
    1. For Trigger configuration, select Alexa.
    2. For Choose an Alexa product, select Alexa Smart Home.
    3. For Skill ID verification, select Enable (recommended).
    4. For SkillID, paste the skill ID that you saved in Step 1: Create a Smart Home Skill.
    5. To add the trigger to invoke your skill, click Add.

Substep 2.3: Add skill code

Now, you add the example skill code to your Lambda function. The example code contains a discovery response to model the light bulb and an Alexa response to control the light bulb. On receipt of a directive from Alexa, the Lambda function executes your skill code.

The discovery response identifies the light bulb endpoint and includes as much identifying information about the endpoint as possible, such as manufacturer, model, and serial number. In addition, the discovery response identifies the capabilities of the light bulb. In this example, the response includes the Alexa.PowerController interface to indicate that the customer can turn the light bulb on and off. For more details about discovery, see About Alexa Discovery.

After the customer asks Alexa to turn the light bulb on, Alexa sends a TurnOn directive to the skill. In response, the skill calls the appropriate interface in the device cloud to turn on the bulb, and then responds to Alexa with the current state of the light bulb endpoint.

A fully functional smart home skill might include a combination of capability interfaces that best represent the features of the endpoint device. And, the skill usually supports state and change reporting to keep Alexa up-to-date on the status of the device. For available Smart Home interfaces that you can use to model your device type, see List of Alexa Interfaces and Supported Languages.

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 or index.mjs. For the tutorial, rename index.mjs to 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.

Substep 2.4: 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 to report the endpoint capabilities 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 Create new event.
  3. For Event name, enter DiscoveryTest.
  4. For Template, from the drop-down menu, select Alexa Smart Home Discovery Request.
  5. In the Event JSON 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.

Define a Power on test

  1. Under Test event, select Create new event.
  2. For Event name, enter PowerOnTest.
  3. For Template, from the drop-down menu, select Alexa Smart Home Control Turn On Request.
  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, on the drop-down menu under Event name, 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. To close the logs, click x.
  5. Repeat Steps 1–4 for PowerOnTest.

Substep 2.5: 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, click 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.

Was this page helpful?

Last updated: Feb 02, 2024