Grato por sua visita. Neste momento esta página está apenas disponível em inglês.

Host a Custom Skill as an AWS Lambda Function

The easiest way to build the cloud-based service for a custom Alexa skill is to use AWS Lambda, an Amazon Web Services offering that runs your code only when it's needed and scales automatically, so there is no need to provision or continuously run servers. You upload the code for your Alexa skill to a Lambda function and Lambda does the rest, executing it in response to Alexa voice interactions and automatically managing the compute resources for you.

About Lambda functions and custom skills

Using a Lambda function for your service eliminates some of the complexity around setting up and managing your own endpoint:

  • You do not need to administer or manage any of the compute resources for your service.
  • You do not need an SSL certificate.
  • You do not need to verify that requests are coming from the Alexa service yourself. Access to execute your function is controlled by permissions within AWS instead.
  • AWS Lambda runs your code only when you need it and scales with your usage, so there is no need to provision or continuously run servers.
  • Alexa encrypts its communications with Lambda utilizing TLS. See also AWS security best practices.
  • For most developers, the Lambda free tier is sufficient for the function supporting an Alexa skill. The first one million requests each month are free. Note that the Lambda free tier does not automatically expire, but is available indefinitely.

AWS Lambda supports code written in several languages, including Node.js, Java, and Python. For some languages, you can edit the code in the inline code editor in the AWS Lambda console. For basic testing, you can invoke your function manually by sending it JSON requests in the Lambda console.

For an overview of AWS Lambda, see What Is AWS Lambda?

The Lambda function must handle the requests sent to the skill by Alexa. See:

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

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

This document covers creating a new Lambda function for a custom skill. If you are using the Smart Home Skill API, you use Lambda to create a skill adapter. See Steps to Create an Alexa Smart Home Skill.

Use a template to create your Lambda function

If you are using the Alexa Skills Kit SDK for Node or Python, you can use a template provided in the AWS Serverless App Repository to create your function. This automatically creates the resources you need for the function to work with Alexa:

  • The Lambda function itself, with starting sample code from the corresponding GitHub repository. You can use this to experiment with the sample functionality, or replace it with your own code.
  • The SDK dependencies needed for the code. This eliminates the need to upload the dependencies (such as node_modules) yourself.
  • The trigger that grants Alexa the necessary invocation permissions for your function.
  • The role for the function. This defines the AWS resources the function can access.

The serverless app repository lets you find applications to deploy. You can think of the "application" in this context as a template for creating a Lambda function.

You deploy an application to create the resources associated with the application in an AWS CloudFormation stack. Once deployed, you can work with your Lambda function normally. You do not need to be familiar with CloudFormation to set up your Lambda function this way.

The serverless application repository currently contains applications for the ASK SDK for Node v2 and ASK SDK for Python.

If you do not already have an account on AWS, go to Amazon Web Services and create an account first.

Use one of these links to open the page for an Alexa sample in the serverless app repository. To use one of these, click the link, then follow the directions in Deploy the application to create your function, later:

Search for an Alexa sample

Alternatively, you can search for Alexa samples in the serverless app repository:

  1. If you do not already have an account on AWS, go to Amazon Web Services and create an account.
  2. Log in to the AWS Management Console and navigate to AWS Lambda.
  3. Click Create function.
  4. Select Browse serverless app repository.
  5. Under Public applications, type Alexa in the search box. Note that the officially provided repos show “Alexa Skills Kit” as the author in the lower left of the card.
  6. Click the name of the app to use. This provides you with a detail page about the app. You can see details about the resources that will be created under “Template”
  7. Follow the directions in Deploy the application to create your function, later.

Deploy the application to create your function

  1. Open the page for the serverless application to deploy. You can either use one of the provided links or search and browse the repository for a sample.
  2. Click the region drop-down in the upper-right corner of the console and select one of the regions supported for Alexa skills:

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

    Typically you want to use the region closest to you or closest to your customers.

  3. In the Application Settings section, optionally modify the following:
    • Application name
    • SkillDescription
    • SkillFunctionName

    Note that SkillDescription and SkillFunctionName are not included on all templates.

    You can deploy with the same template multiple times, but you must use a unique Application name each time.

  4. Click the Deploy button at the bottom of the page.
  5. Wait for the status of all resources to change to CREATE_COMPLETE
  6. Under Resources, click the link to open the newly created function in the Lambda console.

You can now do any of the following:

Create a Lambda function from scratch

You can create a new Lambda function from scratch with the Lambda console. Use this method if you are not using the ASK SDK for Node v2 or ASK SDK for Python.

To create a new Lambda function:

  1. If you do not already have an account on AWS, go to Amazon Web Services and create an account.
  2. Log in to the AWS Management Console and navigate to AWS Lambda.
  3. Click the region drop-down in the upper-right corner of the console and select one of the regions supported for Alexa skills:

    • Asia Pacific (Tokyo)
    • EU (Ireland)
    • US East (N. Virginia)
    • US West (Oregon)
  4. If you have no Lambda functions yet, click Get Started Now. Otherwise, click Create a Lambda Function.
  5. Enter a Name for the function.
  6. Select the Role for the function. This defines the AWS resources the function can access.
  7. Select the language you want to use for the Runtime. If you are using one of the Alexa Skills Kit SDKs, select Node, Python, or Java. For the list of languages supported by Lambda, see AWS Lambda Runtimes in the AWS Lambda documentation.
  8. Click Create function.

You can now do any of the following:

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

View and edit the code for the Lambda function

To view and edit the code, click the name of the function under Designer, then scroll down to the Function code section.

From here, you can do any of the following, depending on the runtime you selected:

  • Write your code directly in the code editor in the Lambda console.
  • Write your code offline and copy and paste it into the Lambda console editor.
  • Write your code offline and upload it to the Lambda function in a zip or jar file.
  • Use the Eclipse IDE and the AWS Toolkit for Eclipse. For details, see Using AWS Lambda with the Toolkit for Eclipse.

For more about the tools you can use to author your function, see Authoring Code for Your Lambda Function in the AWS Lambda documentation.

Connect the Lambda function to your skill

To connect the function with your skill, you need to update the Endpoint field for your skill to the AWS Lambda ARN for your function:

  1. Copy the ARN shown in the upper-right corner of your Lambda function.
  2. Open or create your skill in the developer console.
  3. Navigate to Custom > Endpoint.
  4. For the Service Endpoint Type, select AWS Lambda ARN, then paste the ARN for your Lambda function in the Default Region box.

Configure the trigger for a Lambda function

You must configure at least one trigger for your function to grant Alexa the necessary invocation permissions for your function.

When you add the Alexa Skills Kit as a trigger, it is recommended that you also enable skill ID verification for the function and provide the skill ID (also called the application ID) for your skill. This means your function can be invoked only if the skill ID in the Alexa Skills Kit request matches the skill ID configured in the trigger.

When using skill ID verification, you do not need to include code to verify that the request is intended for your service. Requests from unverified skills do not invoke your function.

Multiple Skills Calling the Same Function

You can configure multiple triggers if you want to invoke the same Lambda function from multiple skills. In this case, each trigger must be configured with a unique skill ID.

Add an Alexa Skills Kit trigger

Before you add the trigger, copy the skill ID from the developer console:

  1. Open or create your skill in the developer console.
  2. Find the skill in the list and click View Skill ID below the skill name. Copy the ID from the popup.

Once you have your skill ID, add the trigger to the function:

  1. Log in to the AWS Management Console and navigate to AWS Lambda.
  2. Click your function in the list to open the configuration details.
  3. Make sure you are on the Configuration page.
  4. In the Designer section, under Add triggers, click Alexa Skills Kit to select the trigger.

    If you do not see Alexa Skills Kit as an option in Add triggers list, this means your Lambda function is not in a region supported by Alexa Skills Kit. Be sure to create the function in one of the supported regions:

    • Asia Pacific (Tokyo)
    • EU (Ireland)
    • US East (N. Virginia)
    • US West (Oregon)
  5. Under Configure triggers, select Enable for Skill ID verification.
  6. Enter your skill ID in the Skill ID edit box.
  7. Click Add.
  8. Click Save to save the change.

It is highly recommended that you limit invocation permissions to just Alexa and enable skill ID verification to protect your function from malicious callers.

Remove an Alexa Skills Kit trigger

You can remove a trigger from your function in the Lambda console. This also removes the skill ID verification for any skill ID included in the trigger.

  1. Log in to the AWS Management Console and navigate to AWS Lambda.
  2. Click your function in the list to open the configuration details.
  3. Make sure you are on the Configuration page. Existing triggers for the function are displayed in the Designer section at the top of the page.
  4. Click the Alexa Skills Kit box in the Designer section to display the Alexa Skills Kit triggers previously added to your skill. They are shown in an Alexa Skills Kit section at the bottom of the page.
  5. Click the Delete button next to each trigger to delete.
  6. Click Save to save the change.

Change an existing trigger

You cannot edit an existing trigger for a Lambda function. To change a trigger (for instance, to add or change the skill ID), remove the old trigger, then add a new trigger.

Configure triggers with the AWS CLI or Lambda API

AWS Lambda provides both an API and command line interface (CLI) for managing Lambda functions. You can use these tools to add or remove the Alexa Skills Kit trigger as well. These let you add a resource-based policy granting the Alexa Skills Kit permission to invoke the function.

For the Alexa Skills Kit trigger, you need to set the following:

  • action must be lambda:InvokeFunction.
  • principal must be alexa-appkit.amazon.com.
  • event-source-token is the ID for your skill.

For example, this CLI command adds the trigger to the Lambda function hello_world:

aws lambda add-permission
    --function-name hello_world
    --statement-id 1
    --action lambda:InvokeFunction
    --principal alexa-appkit.amazon.com
    --event-source-token amzn1.ask.skill.xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

See AddPermission and add-permission for details about the API and CLI.

Triggers added with the API or CLI are displayed in the Lambda console just like all other triggers.

Disable Skill ID verification

You can configure your function to not use skill ID verification if absolutely necessary. For instance, you may want to turn off the verification in an environment in which you have a large number of skills invoking the same function for testing purposes. It is still recommended that you re-enable verification before publishing your skill.

To disable skill ID verification in the Lambda console:

  1. Remove any existing triggers that are configured with skill ID verification
  2. Add a new Alexa Skills Kit trigger as described above and select Disable for Skill ID verification.

To disable skill ID verification with the Lambda API or CLI:

  1. Remove any existing triggers that are configured with skill ID verification. See RemovePermission (API) and remove-permission (CLI) for details about the API and CLI.
  2. Add a new resource-based policy granting the Alexa Skills Kit permission to invoke the function, but do not set the EventSourceToken.

For example, this CLI command adds the trigger to the Lambda function hello_world, but without skill ID verification:

aws lambda add-permission
    --function-name hello_world
    --statement-id 1
    --action lambda:InvokeFunction
    --principal alexa-appkit.amazon.com

See AWS documentation for details about using the Lambda API or CLI.

If you later want to re-enable skill ID verification (recommended before publishing the skill), delete any triggers that don't include the EventSourceToken, then add a new trigger and include the skill ID.

Test a Lambda function in the console

You can manually test a Lambda function in the Lambda console by sending sample JSON events formatted in the same way as requests sent by Alexa. Sample events are provided for testing within the console. You can use these events as a starting point and modify them to represent requests that the Alexa service would send to your own function. Note that you can also send events to a Lambda function using the AWS CLI.

The Lambda console does not check the conditions on the function's triggers, so your test event does not need to have a matching skill ID when you use skill ID verification.

See Request and Response JSON Reference for details about the JSON interface.

To test your function, you need to create a new test event:

  1. In the Function list, click the function name to open the details for the function.
  2. In the upper right corner of the screen, click the Select a test event drop-down list and click Configure test events.
  3. Select Create new test event and then select one of the sample Alexa requests from the Event template list:
    • Alexa Start Session
    • Alexa Intent (multiple samples available)
    • Alexa End Session
  4. Enter an Event name.
  5. Click Create.
  6. On the main function configuration page, make sure your event is selected and click the Test button.

You can configure multiple events to test sending different requests to your function.

After the function runs, the Execution result section shows the response returned by the function, in JSON format. You should see a response appropriate for the request pasted into the Sample event box. For example, for a LaunchRequest, a function built from the alexa-skills-kit-nodejs-premium-facts-skill template returns a response similar to this:

{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "SSML",
      "ssml": "<speak>Welcome to Premium Facts Sample. To hear a random fact you can say 'Tell me a fact', or to hear about the premium categories for purchase, say 'What can I buy'.  For help, say , 'Help me'... So, What can I help you with?</speak>"
    },
    "reprompt": {
      "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>I didn't catch that. What can I help you with?</speak>"
      }
    },
    "shouldEndSession": false
  },
  "userAgent": "ask-node/2.3.0 Node/v8.10.0",
  "sessionAttributes": {}
}

The Log output section shows any log messages generated by the code. The sample writes a log message for each type of request, so you should see something like the following:

2015-05-18T23:53:22.357Z    0f885f98-fdb9-11e4-80af-1b9f8363b496    onIntent requestId=amzn1.echo-api.request.6919844a-733e-4e89-893a-fdcb77e2ef0d, sessionId=amzn1.echo-api.session.abeee1a7-aee0-41e6-8192-e6faaed9f5ef

For additional sample skills you can set up, see: