Deploy a Sample Custom Skill to AWS Lambda
The Alexa Skills Kit provides several samples of custom skills written in Node.js (JavaScript) and Java. An easy way to get started working with these samples is to deploy and test them as AWS Lambda functions on AWS Lambda (a service offering by Amazon Web Services).
The samples are available in GitHub repositories. For details about getting the samples and the specific samples included, see Using the Alexa Skills Kit Samples (Custom Skills).
This document describes how to deploy the samples as Lambda functions. For an overview of AWS Lambda, see What Is AWS Lambda?. For more about using Lambda with the Alexa Skills Kit, see Creating an AWS Lambda Function for a Custom Skill.
If you do not already have an account on AWS, go to Amazon Web Services and create an account before completing the procedures below.
For the Node.js samples, see these sections:
- Preparing a Node.js Sample to Deploy in Lambda
- Creating the Lambda Function for the Sample
- Creating a New Skill for the Sample on the Developer Portal
- Updating the Application ID in the Code for the Sample
For the Java samples, see these sections:
- Preparing a Node.js Sample to Deploy in Lambda
- Preparing a Java Sample to Deploy in Lambda
- Creating the Lambda Function for the Sample
- Creating a New Skill for the Sample on the Developer Portal
- Updating the Application ID in the Code for the Sample
- Next Steps
Preparing a Node.js Sample to Deploy in Lambda
The Node.js samples are available on GitHub in the Alexa organization. The samples use the Alexa Skills Kit SDK for Node.js.
To deploy a Node.js sample as a Lambda function, you first need to create a zip file containing the .js files provided in the sample. The Node.js samples use the Alexa Skills Kit SDK for Node.js, so you need to also include these dependencies in your zip file. If you have not already installed Node.js and NPM, follow these steps first.
Readme.md file in the GitHub repository for the sample you are setting up for any additional dependencies or setup steps.-
Download or clone the GitHub repository for the sample. The Node.js samples are all in separate repositories in the Alexa GitHub organization.
If the sample is available as a Lambda blueprint, you can skip down to step 4 and create the Lambda function. It is still useful to download the repository for easy access to the interaction model files.
-
In a terminal or command line, navigate to the
srcdirectory for the sample and enter this command:bash npm install --save alexa-sdkThis installs a
node_modulesdirectory containing the SDK dependencies. -
Select all the contents of the
srcfolder, and create a zip file.The zip should contain the files, but not include the
srcfolder. Theindex.jsfile must be at the root of the zip. The zip should include thenode_modulesdirectory. - See Creating the Lambda Function for the Sample to create the new function and upload the code.
- See Creating a New Skill for the Sample on the Developer Portal to register the sample skill on the developer portal.
- See Updating the Application ID in the Code for the Sample to update the sample with the newly-generated application ID.
Preparing a Java Sample to Deploy in Lambda
To deploy a Java sample as a Lambda function, you must first build the sample using Maven, then upload the resulting JAR file.
Requirements
-
Apache Maven - The Java samples are set up with a Maven script that builds a
.JARfile containing all the required dependencies. This automatically includes the Java library needed for the sample.Before attempting to build a Java sample, make sure you have a current version of Maven installed. You can download Maven from the Apache Maven page
-
A development environment with Java Development Kit (JDK) 8 or later.
Building the Java Sample
- Download or clone the amzn/alexa-skills-kit-java GitHub repository.
- Open a command line and navigate to the
samplesfolder in the zip. This directory contains thepom.xmlfile. -
Run the following command:
mvn assembly:assembly -DdescriptorId=jar-with-dependencies packageThis creates a
.JARfile calledalexa-skills-kit-samples-1.0-jar-with-dependencies.jarin thetargetdirectory. - See Creating the Lambda Function for the Sample to create the new function and upload the code.
- See Creating a New Skill for the Sample on the Developer Portal to complete the setup so you can test the sample.
- See Updating the Application ID in the Code for the Sample to update the sample with the newly-generated application ID.
Creating the Lambda Function for the Sample
Once you have either a zip containing the Node.js code or a compiled .JAR for the sample, do the following:
- Log in to the AWS Management Console and navigate to AWS Lambda.
- Create a new Lambda function in the US East (N. Virginia) or EU (Ireland) region.
- To quickly set up one of the blueprint samples, select the sample from the list of blueprints. The following samples are available as blueprints:
- alexa-skill-kit-sdk-factskill
- alexa-skill-kit-sdk-howtoskill
- alexa-skill-kit-sdk-triviaskill
- alexa-skills-kit-color-expert
Selecting one of these blueprints automatically imports the code into the Lambda console.
-
Configure the new function with the following settings, depending on whether your are deploying a Node.js or Java sample:
Setting Node Value Java Value Triggers Click the outlined box select Alexa Skills Kit. Click the outlined box select Alexa Skills Kit. Name A descriptive name for the function. A descriptive name for the function. Description A description for the function. A description for the function. Runtime Node.js Java 8 Lambda function code Select the Upload a .ZIP file option and upload the zip file you created.
If you selected one of the blueprints (such asalexa-skill-kit-sdk-factskill), the code is already filled in.Select the Upload a .ZIP file option and upload the file alexa-skills-kit-samples-1.0-jar-with-dependencies.jarin thesamples/targetfolder.Handler index.handlerThe SpeechletRequestStreamHandlerclass implemented for the sample. For example, for the Hello World sample, this would behelloworld.HelloWorldSpeechletRequestStreamHandler. See theReadme.mdfor the specific sample to get this name. - Select the Role for the function. This defines the AWS resources the function can access.
- To use an existing role, select the role under Use existing role.
- To create a new role, see Defining a new Role for the Function, below
- Make note of the Amazon Resource Name (ARN) for your new Lambda function. The ARN is displayed in the upper-right corner of the function page.
- Continue with the next step: create a new skill for the sample on the developer portal.
Defining a New Role for the Function
The role specifies the AWS resources your function can access. To create a new role while configuring your function:
- For Role (under Lambda function handler and role), select Create new role from template(s).
- Enter the Role Name.
- From the Policy templates list, select Simple Microservice permissions.
Creating a New Skill for the Sample on the Developer Portal
Register a new Alexa skill on the developer portal. For details, see Registering and Managing Custom Skills in the Developer Portal.
Use the following information when registering the new skill:
| Setting | Value |
|---|---|
| Invocation Name | Any valid invocation name. |
| Name | Any valid name. |
| Endpoint | Select the AWS Lambda ARN (Amazon Resource Name) option, then either North America or Europe and enter the ARN for your function. |
| Interaction Model | Each sample includes an interaction model in the speechAssets folder for the sample.Copy the JSON from IntentSchema.json into the Intent Schema box and copy the text from SampleUtterances.txt into the Sample Utterances box.For details about defining the interaction model, see Define the Interaction Model in JSON and Text. |
Updating the Application ID in the Code for the Sample
Once the sample is registered as a skill, you need to update the Application ID in the code of the sample to match the ID assigned to the new skill. This ensures that the Lambda function only accepts requests sent by the skill you registered.
- Log on to the Developer Portal.
- Navigate to the Alexa section by clicking Apps & Services and then clicking Alexa in the top navigation.
- In the Alexa Skills Kit box, click Get Started.
- Find the skill in the list and click Edit.
- On the Skill Information page, copy the Application Id shown.
- Update code for the sample with the ID you copied:
- For a Java sample, specify the application ID with a
supportedApplicationIds.add()method call in theSpeechletRequestStreamHandlerclass for the sample. - For a Node.js sample, specify the application ID in the
APP_IDvariable at the beginning of theindex.jsfile.
- For a Java sample, specify the application ID with a
- Upload your updated code to your Lambda function.
- For Node.js, create an updated zip file for your code and upload to your Lambda function.
- For Java, rebuild the sample JAR file for your code and upload it to your Lambda function.