Gracias por tu visita. Esta página solo está disponible en inglés.

Step 6: Create and Deploy a Lambda Package (VSK Fire TV)

Now that you have your Fire TV app uploaded, you need to create a Lambda function with capabilities to pass the directives received from Alexa along to your app. A sample Lambda function that provides a starting point for implementing a video skill is provided below. This sample Lambda will respond to the directives from Alexa and communicate with your Fire TV app via ADM.

About the Lambda

In this step, you'll start working with Lambda. The documentation here includes sample Lambda code for a VSK integration with your Fire TV app, including responses for Discovery, Search and Play, Fast-forward, Channel Change, and other directive types. You will use this index.js code as a starting point but then later add your own logic.

You need to update this Lambda function with a client secret and client ID. You will use a Node JS application to generate the function as a Lambda deployment package (packaged as a zip file), which you will then upload into AWS Lambda. Later, you will customize this function with the logic needed to handle the incoming directives and pass them along to your app for processing.

As you customize your Lambda function's logic, it will help to have some familiarity with Node.js, though expertise in this area is not required at this stage (and you can code your Lambda in a number of other languages). For this sample Lambda, just make sure that you have at Node.js version 10 or later. You can check if Node and NPM are already installed by typing node -v and npm -v. If you don't already have Node and npm, install them. More information about handling the expected Alexa directives in your Lambda function is provided in Interpret and React to Directives.

Download the Sample Lambda

Download firetv-lambda.zip and unzip the file. This project contains a sample Lambda file (index.js) with Node package files that will download Node modules needed to generate the Lambda code as a deployment package.

Update Client ID and Client Secret in the Lambda Function

First, you need to update the client ID and secret in the sample Lambda code:

  1. In a text editor, browse to the firetv-lambda folder and open the index.js.
  2. Enter the values for the CLIENT_ID and CLIENT_SECRET that you retrieved in Sign Your App and Configure a Security Profile:

    const CLIENT_ID = '<enter client id>';
    const CLIENT_SECRET = '<enter client secret>';
    
  3. Open a terminal window or command prompt and browse to the directory that contains the firetv-lambda app. For example:

    cd /Users/<your username>/skills/firetv-lambda/
    
  4. Run the following command:

    npm install
    

    This creates a folder called node_modules (if not already present) and adds all the dependent packages in it. (Don't worry if you see warnings when running this command.)

Create a Lambda Deployment Package

With the Lambda customized and the Node modules installed, it's time to generate a Lambda deployment package:

  1. Browse to the directory with your Lambda code.
  2. Run the following command to create a Lambda deployment package:

    zip -r firetv-lambda.zip .
    

    This creates a zip file with all the Lambda code packaged together. (On Windows, you can also right-click on the folder in Windows Explorer and choose Send To -> Compressed (Zipped) Folder.)

    You will now see a package called firetv-lambda.zip in this directory. You will upload this package into AWS Lambda in an upcoming section (after creating the IAM role).

Create an IAM Role for your Lambda

You need to define permissions that specify which applications can invoke your Lambda function. Here you'll create an IAM role that allows Lambda functions to call AWS services, such as Cloudwatch, on your behalf. To create the IAM role:

  1. Sign in to the AWS Managment Console.
  2. Click Services and go to IAM (use the Search box).
  3. Select Roles from left navigation, and then click Create role.
  4. Under AWS service (selected by default), click Lambda, and then click Next: Permissions at the bottom.
  5. Search for AWSLambdaBasicExecutionRole and select the check box next to it. Then click Next: Tags.
  6. Skip the "Add tags (optional)" configuration and click Next: Review.
  7. Give your role a name (for example, "video_skill_streamz") and description as you like, and click Create role.

    Creating an IAM role that can interact with your Lambda
    Creating an IAM role that can interact with your Lambda

    Make a note of your IAM role name (especially if you have multiple IAM roles in AWS) so that you can easily remember it for the next step.

Create the Lambda Function in AWS for Your Video Skill

Now you'll create the Lambda function in AWS that your video skill will use. To create the Lambda function for your video skill:

  1. Log in to the AWS Management Console.
  2. Click Services and go to Lambda (use the Search box).
  3. In the upper-right, select the appropriate AWS region based on the table provided in AWS Regions and Video Skills. (Unlike with IAM, Lambda functions are specific to AWS regions.)

    Selecting an AWS region
    Selecting an AWS region

    Selecting a region close to your customers will ensure that the latency between customer utterances and Lambda invocations is minimal. Also, the video skill won't work if you select the wrong AWS region.

  4. Click Create function.
  5. Leave Author from scratch selected as is, and then complete the following:
    • Function name: A unique name for your Lambda function (for example, firetv_app_lambda.
    • Runtime: Node.js 10.x (The sample Lambda code is written in Node.js. Later, you can explore using other languages for your Lambda, such as Python, Ruby, .NET, Java, etc.)
    • Permissions
      Creating a basic Lambda function in AWS
      Creating a basic Lambda function in AWS
  6. Click Create function.

    Your Lambda function is created. Note that your Lambda function will not receive directives from Alexa unless your add a Smart Home trigger that tells the Lambda where it should get the message from. (This is what the Smart Home API does — it connects your devices to Lambda computing in the cloud.)

  7. Click + Add trigger.
  8. In the Trigger configuration, select Alexa Smart Home and then enter your video skill ID (created in Step 1: Create Your Video Skill and Set Up Devices) in the Application ID field. Then click Add.

    Configuring Lambda with your Video Skill ID
    Configuring Lambda with your Video Skill ID

    After you click Add, you see a success notification at the top that says, "The trigger 123456789 was successfully added to function [Lambda function name]. The function is now receiving events from the trigger." In this case, the trigger for the Lambda function is your video skill.

  9. The ARN for your Lambda function appears in the upper-right corner of the page, as shown in the following screenshot. Copy your Lambda's ARN and make a note of it. You will need this info when you update your video skill in an upcoming step.

    Location of the ARN for your Lambda function
    Location of the ARN for your Lambda function
  10. Click your Lambda function's name (the box with Lambda icon ) in the center of the page. This will expand a Function Code section in the pane below.
  11. In the Function Code section, click the Code entry type menu and select Upload a .zip file.
  12. Click Upload and select the firetv-lambda.zip zip file that you created early through Node (Create a Lambda Package).
  13. Click Save.

More about Lambda

Remember that Alexa listens for customer phrases (e.g., "Play Interstellar") and packages up the request as a directive (a JSON object). The Lambda function will receive directives sent from Alexa and take some action. Your Lambda determines the request type by identifying the namespace and name values in the directive's message header. For example, the namespace specifies whether the request is a Discover or ChannelChange directive. Depending on this namespace, either the handleDiscovery or handleChannelControl function is called.

The sample Lambda provides basic handling of directives. When you customize the Lambda code, you will need to handle every type of request a user would make to your skill and provide the correct response in your app through Amazon Device Messaging (ADM). Later in the tutorial, you will test your video skill for the way it handles various directives.

Next Steps

Continue on to the next step: Step 7: Update the Fire TV Skill Sections.

(If you run into any issues that prevent you from continuing, see Troubleshooting.)