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:

For the Java samples, see these sections:


Prepare 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.

  1. 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.

  2. In a terminal or command line, navigate to the src directory for the sample and enter this command:

     npm install --save alexa-sdk
    

    This installs a node_modules directory containing the SDK dependencies.

  3. Select all the contents of the src folder, and create a zip file.

    The zip should contain the files, but not include the src folder. The index.js file must be at the root of the zip. The zip should include the node_modules directory.

  4. See Set Up a New Skill in the Developer Console and Get the Skill ID to create the new skill and get configuration information.
  5. See Creating the Lambda Function for the Sample to create the new function and upload the code.
  6. See Complete the Configuration of the Skill on the Developer Console to connect the Lambda function with the skill.
  7. See Updating the Application ID in the Code for the Sample to update the sample with the newly-generated application ID.

Prepare 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 .JAR file 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.

Build the Java Sample

  1. Download or clone the alexa/alexa-skills-kit-sdk-for-java GitHub repository.
  2. Open a command line and navigate to the samples folder in the zip. This directory contains the pom.xml file.
  3. Run the following command:

     mvn assembly:assembly -DdescriptorId=jar-with-dependencies package
    

    This creates a .JAR file called alexa-skills-kit-samples-1.0-jar-with-dependencies.jar in the target directory.

  4. See Set Up a New Skill in the Developer Console and Get the Skill ID to create the new skill and get configuration information.
  5. See Creating the Lambda Function for the Sample to create the new function and upload the code.
  6. See Complete the Configuration of the Skill on the Developer Console to connect the Lambda function with the skill.
  7. See Updating the Application ID in the Code for the Sample to update the sample with the newly-generated application ID.

Set Up a New Skill in the Developer Console

Configure a new skill in the developer console with basic information. This generates a skill ID that you can use when you configure the Lambda function.

Log on to the developer console and create a new custom skill as described in Create a Skill and Choose the Interaction Model. Enter just the following information:

Setting Value
Skill Name Any valid name.
Invocation Name Any valid invocation name.
Model Custom

Return to the list of skills and note the ID displayed below the skill name.

Create the Lambda Function for the Sample

Lambda functions for Alexa skills can be hosted in any of one of these regions:

  • Asia Pacific (Tokyo)
  • EU (Ireland)
  • US East (N. Virginia)
  • US West (Oregon)

Once you have either a zip containing the Node.js code or a compiled .JAR for the sample, do the following:

  1. Log in to the AWS Management Console and navigate to AWS Lambda.
  2. Create a new Lambda function and select one of the regions supported for Alexa skills: Asia Pacific (Tokyo), EU (Ireland), US East (N. Virginia), or US West (Oregon).
  3. To quickly set up one of the blueprint samples, select the sample from the list of blueprints. The following samples are available as blueprints:

    Selecting one of these blueprints automatically imports the code into the Lambda console.

  4. Configure the new function with the following settings, depending on whether you are deploying a Node.js or Java sample:

    Setting Node Value Java Value
    Triggers Alexa Skills Kit. Enter the Skill ID under Configure triggers. Alexa Skills Kit. Enter the Skill ID under Configure triggers.
    Name A descriptive name for the function. A descriptive name 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 as alexa-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.jar in the samples/target folder.
    Handler index.handler The SpeechletRequestStreamHandler class implemented for the sample. For example, for the Hello World sample, this would be helloworld.HelloWorldSpeechletRequestStreamHandler. See the Readme.md for the specific sample to get this name.
  5. Select the Role for the function. This defines the AWS resources the function can access.
  6. 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.
  7. Continue with the next step: create a new skill for the sample on the developer console.

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:

  1. For Role (under Lambda function handler and role), select Create new role from template(s).
  2. Enter the Role Name.
  3. From the Policy templates list, select Simple Microservice permissions.

Complete the Configuration of the Skill in the Developer Console

Return to the developer console and edit the skill you started earlier. Complete the rest of the configuration:

Setting Value
Build page > Custom > Interaction Model Each sample includes an interaction model in JSON format. In the developer console, click Custom > Interaction Model > JSON Editor to open the JSON Editor. Paste the JSON from the sample into the JSON Editor, replacing the default JSON.

For details about defining the intents and utterances, see Create Intents, Utterances, and Slots.
Build page > Custom > Endpoint Select the AWS Lambda ARN option, then enter the ARN for your function in the Default Region box.

Click Build Model to build the model for the skill.