Build a Smart Home Skill
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.
- Set up permissions
- Create an IAM role
- Create a Lambda function
- Add skill code
- Test the Lambda function
- Copy the resource name
Substep 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
- Sign in to the AWS management console.
- Under AWS Services > All services > Security, Identity, & Compliance, click IAM.
Or, go directly to the IAM dashboard. - On the IAM dashboard, from the left menu under Access management, click Policies.
- On the Policies page, click Create Policy.
- On the Create policy page, select the JSON tab.
- Copy and paste the following policy definition into the text area.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
- Click Next: Tags.
- Click Next: Review.
- Under the Create policy > Review policy, for Name, enter smart_home_skill_policy.
You can leave the description blank. - To create the policy, click Create policy.
Now, you can see the smart_home_skill_policy in the list of available policies.
Substep 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
- On the IAM dashboard, from the left menu under Access management, click Roles.
- On the Roles page, click Create role.
- On the IAM > Roles > Create role page, enter the following information:
- Under Select trusted entity, for Trusted entity type, select AWS service.
- Under Common use cases, select Lambda.
- Click Next.
- On the Add permissions page, filter to find the smart_home_skill_policy policy that you just created.
- Select, click the check box next to smart_home_skill_policy policy,and then click Next.
- 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.
Substep 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
- Navigate to the Lambda console.
- On the Lambda console, click Create function.
If you've created Lambda functions, the console displays a list of your functions. - Click Create function.
Note: In the upper-right corner, the console displays the AWS region where the Lambda function resides. This tutorial uses the North American region, AWS N. Virginia (us-east-1) region, and English (US). If you want to run your skill outside of North America, select a different AWS region and language. To learn more, see Develop Smart Home Skills for Multiple Languages.
- Under Create function, select Author from scratch.
- Under Basic information, enter the following information:
- For Function name, enter my-smart-home-skill.
- For Runtime, choose Python 3.9 or Node.js 14.x.
- For Architecture, leave the default.
- For Permissions > Change default execution role, select Use an existing role.
- For Existing role, select lambda_smart_home_skill_role, and then select Create function.
- On the Lambda > Functions > my-smart-home-skill page, under Function overview, click + Add trigger.
- On the Add trigger page, enter the following information about to trigger your skill:
- For Trigger configuration, select Alexa Smart Home.
- For Application ID, paste the skill ID that you saved in Step 1: Create a Smart Home Skill.
- To add the trigger to invoke your skill, click Add.
Substep 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
- 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. - To display the function code, click lambda_function.py or index.js.
- Replace the existing code with the following smart home skill code that controls the virtual light bulb.
The code supports theAcceptGrant
,Discovery
,TurnOn
, andTurnOff
directives.
- To save the file, under File, select Save.
- 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 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
- On the Lambda > Functions > my-smart-home-skill page, select the Test tab.
- Under Test event, select Create new event.
- For Event name, enter DiscoveryTest.
- For Template, from the drop-down menu, select Alexa Smart Home Discovery Request.
- In the code editor, replace the existing code with the following test code.
{
"directive": {
"header": {
"namespace": "Alexa.Discovery",
"name": "Discover",
"payloadVersion": "3",
"messageId": "1bd5d003-31b9-476f-ad03-71d471922820"
},
"payload": {
"scope": {
"type": "BearerToken",
"token": "access-token-from-skill"
}
}
}
}
- To save the test code, click Save changes.
Define a Power on test
- Under Test event, select Create new event.
- For Event name, enter PowerOnTest.
- For Template, from the drop-down menu, select Alexa Smart Home Control Turn On Request.
- In the code editor, replace the existing code with the following test code.
{
"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": {}
}
}
- To save the test code, click Save changes.
To test your Lambda function on the AWS developer console
- To open the test, under Test event, on the drop-down menu under Event name, select DiscoveryTest.
- To run the test, under Test event, click Test.
If the test is successful, you see Execution result: succeeded. - To view the logs, in the Execution result box, expand Details.
- To close the logs, click x.
- Repeat Steps 1–4 for PowerOnTest.
Substep 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.

To copy the Lambda function ARN in the AWS developer console
- At the top of the Lambda > Functions > my-smart-home-skill page, select Copy ARN.
- 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.
Last updated: Oct 14, 2022