Developer Console

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 through 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 (generated 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 too). For this sample Lambda, you just need Node.js version 10 or later. More information about handling the expected Alexa directives in your Lambda function is provided in Interpret and React to Directives.

Download the Sample Lambda

The sample Fire TV Lambda Function is included in the sample-fire-tv-app-video-skill GitHub repo, inside the Lambda folder. You downloaded this folder when you downloaded the sample app in Step 2, Download Sample Fire TV App with VSK.

This Lambda folder contains a sample Lambda file, index.js, along with a package.json file to download Node modules needed to generate the Lambda code as a deployment package. The folder also contains video_catalog.json that provides an example of a data source for the video content.

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. Make sure you have Node and NPM version 10 or later installed. You can check for Node by typing node -v. You can check for npm by typing npm -v. If you don't already have them installed, download and install Node JS. (Your Node JS installation will include npm, so you don't need to install npm separately from Node JS.)
  2. In a text editor, browse to the Lambda folder and open index.js.
  3. Enter the values for the CLIENT_ID and CLIENT_SECRET that you retrieved in Sign Your App and Configure a Security Profile:

    lines 32-33:

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

    cd /Users/<your username>/sample-fire-tv-app-video-skill/Lambda
    
  5. Run the following command:

    npm install
    

    This creates a folder called node_modules and adds all the dependent packages in it. It also adds package-lock.json You'll see a success message such as the following:

    added 69 packages from 127 contributors and audited 89 packages in 5.483s
    found 0 vulnerabilities
    

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 index.js 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. (It's not required to use your same Amazon Developer account as before.)
  2. Click Services in the top navigation bar search for IAM, then click IAM in the results.
  3. Select Roles from left navigation, and then click the Create role button.
  4. Under the AWS service rectangle (selected by default), click Lambda, and then click the Next: Permissions button at the bottom.
  5. Search for AWSLambdaBasicExecutionRole and select the check box next to it. Then click the Next: Tags button.
  6. Skip the "Add tags (optional)" configuration and click the Next: Review button.
  7. Give your role a name (for example, "video_skill_streamz") and description as you like, and click the Create role button.

    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 following table: (Unlike with IAM, Lambda functions are specific to AWS regions.)

    Locale Customer Region AWS Region
    en-US, en-CA, fr-CA, pt-BR, es-MX North America US East (N. Virginia)
    en-GB, fr-FR, de-DE, it-IT, es-ES, en-IN, hi-IN Europe EU (Ireland)
    ja-JP, en-AU Far East (FE) US West (Oregon)

    (In the locale abbreviations, the lowercase letters indicate the language, the uppercase indicate the region. For example, "fr-CA" refers to French-speaking Canada, whereas "en-CA" refers to English-speaking Canada.)

    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.

    Selecting an AWS region
    Selecting an AWS region
  4. Click the Create function button.
  5. Leave the Author from scratch rectangle 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. Expand this section by clicking Choose or create an execution role. Then complete the following:
      Creating a basic Lambda function in AWS
      Creating a basic Lambda function in AWS
  6. Click the Create function button.

    Your Lambda function is created. Note that your Lambda function will not receive directives from Alexa unless you 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, such as an Echo Dot, to Lambda computing in the cloud.)

  7. Click the + Add trigger button.
  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 the Add button.

    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. Your video skill will invoke this Lambda function.

  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. The zip file uploads after you 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 ChangeChannel 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 for Cloudside Integrations.)


Last updated: Oct 13, 2021