Substeps to implement add-on code in the AWS console
Complete the following substeps to implement the code that controls a virtual light bulb.- Create an IAM role
- Create a Lambda function
- Add add-on code
- Test the Lambda function
- 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- Sign in to the AWS management console.
- Open the IAM Dashboard.
- On the IAM Identity Center navigation pane, under Access management, choose Roles.
- On the IAM > Roles page, choose Create role.
-
On the IAM > Roles > Create role page, enter the following information:
- For Trusted entity type, select AWS service.
- Under Use cases, select Lambda, and then choose Next.
- Under Add permissions, use the filter to find the policy called AWSLambdaBasicExecutionRole.
- Select the check box next to the AWSLambdaBasicExecutionRole policy and AWS managed type, and then choose Next.
- Under Name, review, and create page, for Role name, enter lambda_smart_home_add-on_role, and then, at the bottom of the page, choose Create role.
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 add-on code. To create an AWS Lambda function in the AWS developer console-
Navigate to the Lambda console.
If you’ve created Lambda functions, the console displays a list of your functions. - On the Functions page, choose Create function.
- Select Author from scratch.
-
In the Basic information pane, enter the following information:
- For Function name, enter my-smart-home-add-on.
- For Runtime, choose a version of Python or Node.js.
- 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 choose Create function.
- On the Lambda > Functions > my-smart-home-add-on page, under Function overview, choose + Add trigger.
-
On the Add trigger page, enter the following information to trigger your add-on:
- For Trigger configuration, select Alexa.
- For Choose an Alexa product, select Alexa Smart Home.
- For SkillID, paste the skill ID that you saved in Step 1: Create a Smart Home Add-on.
- To add the trigger to invoke your add-on, choose Add.
Substep 2.3: Add add-on code
Now, you add the example add-on 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 add-on 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 theAlexa.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 add-on. In response, the add-on 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 add-on might include a combination of capability interfaces that best represent the features of the endpoint device. And, the add-on 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 code to your Lambda function
- On the Lambda > Functions > my-smart-home-add-on page, to view the code editor, select the Code tab.
- Choose the language for your add-on code. For Node.js, your add-on code resides in the file, index.js or index.mjs. For the tutorial, rename index.mjs to index.js.
For Python, your add-on code resides in the file, lambda_function.py. - Open index.js (Node.js) or lambda_function.py (Python).
- In the Code source pane, paste the Node.js or Python code into the code editor, replacing any existing code. The example smart home add-on code supports the
AcceptGrant,Discovery,TurnOn, andTurnOffdirectives for the virtual light bulb.
Example smart home add-on code
Example smart home add-on code
- Node.js
- Python
- To build and load the function, under DEPLOY, choose Deploy.
Make sure to deploy the function after each update that you make.
Substep 2.4: Test the Lambda function
After you deploy your add-on code, you can invoke the Lambda function and view the results in the AWS developer console. The following code examples send anAlexa.Discovery request to report the endpoint capabilities and an Alexa.PowerController request to turn on the light.
Define a discovery test
- On the Lambda > Functions > my-smart-home-add-on page, choose the Test tab.
- Under Test event action, select Create new event.
- For Event name, enter DiscoveryTest.
- For Template, expand the dropdown list, and then select Alexa Smart Home Discovery Request.
- In the Event JSON code editor, replace the existing code with the following test code.
- To save the test code, choose Save.
- Under Test event, select Create new event.
- For Event name, enter PowerOnTest.
- For Template, expand the dropdown list, and then select Alexa Smart Home Control Turn On Request.
- In the code editor, replace the existing code with the following test code.
- To save the test code, choose Save changes.
- To open the test, under Test event, expand the dropdown list under Event name, and then select DiscoveryTest.
- To run the test, under Test event, choose 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, choose x.
- Repeat Steps 1–4 for the PowerOnTest event.
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 add-on 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-add-on page, choose 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.

