Create Your Video Skill and Set Up Devices
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
- Download the Sample Lambda
- Update Client ID and Client Secret in the Lambda Function
- Create a Lambda Deployment Package
- Create an IAM Role for your Lambda
- Create the Lambda Function in AWS for Your Video Skill
- More about Lambda
- Next Steps
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 the sample Fire TV Lambda Function:
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. After downloading, unzip the file.
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:
- In a text editor, browse to the
firetv-lambdafolder and open the
Enter the values for the
CLIENT_SECRETthat you retrieved in Sign Your App and Configure a Security Profile:
const CLIENT_ID = '<enter client id>'; const CLIENT_SECRET = '<enter client secret>';
Open a terminal window or command prompt and browse to the directory that contains the
firetv-lambdaapp. For example:
cd /Users/<your username>/skills/firetv-lambda/
Run the following command:
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:
- Browse to the directory with your Lambda code.
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.zipin 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:
- Sign in to the AWS Managment Console.
- Click Services and go to IAM (use the Search box).
- Select Roles from left navigation, and then click Create role.
- Under AWS service (selected by default), click Lambda, and then click Next: Permissions at the bottom.
- Search for AWSLambdaBasicExecutionRole and select the check box next to it. Then click Next: Tags.
- Skip the "Add tags (optional)" configuration and click Next: Review.
Give your role a name (for example, "video_skill_streamz") and description as you like, and click Create role.
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:
- Log in to the AWS Management Console.
- Click Services and go to Lambda (use the Search box).
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 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.Note: Alexa support for video skills in some of these locales is still forthcoming. See Supported Countries for details.
- Click Create function.
- Leave Author from scratch selected as is, and then complete the following:
- Function name: A unique name for your Lambda function (for example,
- 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.)
Tip: Make a note of your Lambda name, as you will need to reference this name in upcoming steps.
- Execution role: Use an existing role.
- Existing role: Select the IAM role that you created in the previous section (Create an IAM Role for your Lambda).
- Function name: A unique name for your Lambda function (for example,
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.)
- Click + Add trigger.
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.
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.Note: If you're using "versioning" for your Lambda function, you will need to re-add the Smart Home Trigger each time that a new version is created.
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.
- Click your Lambda function's name (the box with ) in the center of the page. This will expand a Function Code section in the pane below.
- In the Function Code section, click the Code entry type menu and select Upload a .zip file.
- Click Upload and select the
firetv-lambda.zipzip file that you created early through Node (Create a Lambda Package).
- 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
ChannelChange directive. Depending on this namespace, either the
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.
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.)