Alexa Node JS skill Serverless Integration

Renjith P
Renjith P
Jan 8 · 7 min read

We will be discussing about deploying Alexa skill service in AWS using Serverless framework which will be addressing the common hurdles developers face.

To start off, you need a basic understanding of Alexa skill, Node JS and AWS Lambda because this is not a beginners tutorial.If you are a beginner in Alexa skill development you may refer the links below given to have better understanding on alexa skill development and deployment.

  1. Alexa Custom Skill Development
  2. Deploy your Alexa skill in AWS Lambda

You might have seen that the hard part with skill service is its deployment. Each and every time developers need to take the zip of the skill service and upload it manually in Lambda server. For every minor change we may be repeating the same.

However there are number of npm modules available for build automation, none fully address the real scenario which make sense of serverless development.

But when Serverless Framework come into action lot more such issues developers faced were addressed.

Serverless is a framework that allows you to create backend applications using the infrastructure existing on the cloud (AWS, Azure, Google Cloud). Thus, you can focus on the more relevant things of your application, like business logic, features and the interaction between the modules of your software.

Let us see how we can make use of Serverless Framework for alexa skill development.

Create Hello World Skill

You need to create “Hello World” skill that will be used for integrating with Serverless framework.

Prerequisites

  • A node js development environment.
  • An npm project. Please refer this link for creating npm projects
  • An Amazon Developer account. This is required to create and configure Alexa skills.

Add ASK SDK

The ASK SDK v2 for Node.js is an open-source Alexa Skill Development Kit. ASK SDK v2 for Node.js makes it easier for you to build highly engaging skills, by allowing you to spend more time on implementing features and less on writing boiler-plate code.

To use the ASK SDK package, simply run the following command in terminal:

npm install --save ask-sdk

Configure Custom Skill

First go to developer.amazon.com and from there to amazon alexa.

Now click Alexa Developer Console from the dropdown menu seen in the top right side corner of the page.You will end up with this login page.

You will ask for login by giving username and password. If you have one use that credentials to login.Otherwise create a new amazon account.

Click Create Skill as show in the figure

Enter the skill name that the users will see when you publish the skill in skill store.For instance we could name it as “My First Skill”.

You may change the default language if required.In my case I choose English(IN)

There are many ways to start building a skill.In our case select the Custom from the models given to start develop a custom skill.

Since we are going to run the skill service locally select Provision your own as the backend resource method.

Now click Create Skill from the top right corner of the page to proceed with skill development.

You may be asked for choose a template.Choose Start from scratch as your template and proceed.

Creating Request Handlers

Now we are going to create different request handlers to handle incoming requests.

Launch Request Handler

The code below given shows how to configure a handler that will be invoked when the skill receives a LaunchRequest. The LaunchRequest event occurs when the skill is invoked without a specific intent.

Create a file called index.js and paste the code.

const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Welcome to the Alexa Skills Kit, you can say hello?';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello User', speechText)
.getResponse();
}
}

The canHandle function returns true if the incoming request is a LaunchRequest. The handle function generates and returns a basic greeting response.

HelloWorldIntent Handler

The following code will be invoked when the skill receives HelloWorldIntent.

Paste the code after LaunchRequestHandler in index.js file

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
},
handle(handlerInput) {
const speechText = 'Hello World!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Hello World!', speechText)
.getResponse();
}
};

Lambda Handler

Add the following code at the begining of index.js file.

'use strict';
const Alexa = require('ask-sdk-core');

Now add this piece of code at the end of file

exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(LaunchRequestHandler,
HelloWorldIntentHandler)
.lambda();

This code creates a Lambda handler function to route all inbound request to your skill.


Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com


Skill Configuration

Create Skill Invocation Name from the Invocation option from the sidebar.In our case we will set it as “my skill”.

Click “Save Model” to save the changes.

Now we need to create a HelloWorldIntent. Click the Add buttonseen in the sidebar near to Intents option.

Leave intent name as HelloWorldIntent .Next add some sample utterances in the intent detail page that will be used for invoking the HelloWorldIntent.

Then click Save Model to save the interaction model.

Serverless Integration

To start we’ll have to install Serverless so that we can use the CLI tools to create, configure and deploy our application.

npm install serverless -g

After installing we can check that the package was correctly installed by using serverless -v .

Set Up AWS Credentials

Go to your AWS console and open the IAM Service. On the lefthand side, click Users and then Add User

Give your user a name and tick the Programmatic access box. This is the type of user that doesn’t have access to the Console, but can be used to access AWS services through an API and Secret keys.

Give the Administrator Access permission to your user. That’s enough permission for all we need.

Make sure to copy your Access key ID and Secret access key to a safe place, we’re gonna use them now.

Back to your terminal, tell Serverless what your AWS credentials by running the following command, and you’ll be good to go. Replace YOUR_ACCESS_KEY_ID and YOUR_SECRECT_ACCESS_KEY with your AWS credentials.

serverless config credentials --provider aws --key YOUR_ACCESS_KEY_ID --secret YOUR_SECRECT_ACCESS_KEY

Define Severless.yml

Create the serverless.yml file in the project root directory and you will need to describe the setup and the resources you need for the Lambda function, the framework version, stage, and region.

service: HelloWorldprovider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-east-1
functions:
alexa:
handler: index.handler
events:
- alexaSkill: amzn1.ask.skill.xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx

Paste the code above in serverless.yml and replace alexaSkill with your skill id from alexa console.

  • Go to developer.amazon.com and from there to amazon alexa.
  • Now click Alexa Developer Console from the dropdown menu seen in the top right side corner of the page
  • This opens the developer console and displays skills you have created.
  • Find the skill in the list. The ID is displayed below the skill name.

You can define more properties in the serverless.ymlfile based on your requirement.Please refer this link for know more about Serverless framework.

Finally in order to deploy the endpoint simply run serverless deploy

Configuring Amazon Alexa Skill

Under Endpoint select AWS Lambda ARN and paste in the ARN of the function you created via serverless deploy. The rest of the settings can be left at their default values.

Finally click Save Endpoints and your skill is ready to test.

Test Your Skill

We are all set to test the skill developed and deployed locally. Make sure that the Test is enabled for this skill option is enabled. You can use the Test page to simulate requests, in text and voice form. Use the invocation name along with one of the sample utterances. For example, “tell my skill to say hi” should result in your skill responding with “Hello world”.

Happy Coding :)


Something just for you

Voice Tech Podcast

Voice technology interviews & articles. Learn from the experts.

Renjith P

Written by

Renjith P

Software Engineer

Voice Tech Podcast

Voice technology interviews & articles. Learn from the experts.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade