How to build a Serverless Alexa Skill

When I was a kid, I was intrigued by the Starship Enterprise’s onboard computer featured in the science fiction series Star Trek. Although cheeky at times in its portrayal of technologies beyond our imagination, the voice-controlled computer always made me wonder. And, here we are in the same lifetime, realizing similar technologies — inside our homes, on a small device… Amazing, I think! 🖖

Let’s build ourselves a custom skill for Alexa. Let me show you how.

header image
header image

[Photo by Andres Urena on Unsplash]

In this post, we will build an Alexa skill to get meetup information. We will be using the Serverless Framework and the serverless-alexa-skills plugin.

We will cover:

  • Creating a project
  • Setting up the plugin
  • Creating an Alexa skill
  • Building the Interaction model
  • Creating Lambda functions for Intents
  • Building and deploying the skill
  • Testing the skill

We have quite a bit of ground to cover so let’s get started.

Create the project

To start off, let’s create a serverless project using the template:

Update 11/29/18: One of the readers ‘mitravinda MK’ reported that using the Serverless Framework v.1.33.0 did not work for him. It worked when he downgraded to v1.32.0. See comments for details.

Add the Alexa skills plugin

Next, we will use the serverless-alexa-skills plugin by Masashi Terui. It enables us to manage Alexa skills via the Serverless Framework. That is big deal. Without the plugin, you would have to set up the Alexa skill manually using the Amazon Alexa Skill Developer console.

Let’s install the plugin by running:

This will add the plugin to the plugins section of the file:

Under the hood, the plugin uses the Alexa Skill Management API. To be able to use the API, we will need to set up credentials with Amazon and authenticate with Amazon.

Set up credentials

Login with Amazon is an OAuth2.0 single sign-on (SSO) system using your Amazon.com account.

Get your credentials, by logging into the Amazon Developer Console. Then, go to Login with Amazon from APPS & SERVICES menu, and then click on Create a New Security Profile. Then, go to Web Settings menu item to create a new security profile.

Leave the empty. Enter in .

Write down your , and the for the new security profile. You can find your as well. You can then set environment variables for each one of these secrets and then reference them in the file as shown below:

Note: If you change the port number from the default , then make sure to add an attribute of to the block in the file, as shown above.

Authenticating with Amazon

This will open the Amazon.com login page in your browser and redirect you to after authenticating. If the authentication is successful, you'll see the message: "Thank you for using Serverless Alexa Skills Plugin!!". The authentication call returns a security token that is used in subsequent calls made by the plugin.

Note: If you get an error in any of the commands, please execute the command again. This is because the security token expires in 1 hour.

Create an Alexa Skill

With the setup done, we can now go ahead and create an Alexa skill.

where:

  • name: Name of the skill
  • locale: Locale of the skill ( for English)
  • type: Type of the skill ( or or )

In our case, to create a new Alexa skill, run:

Note: The for the newly created skill is printed out.

Skill Manifest

Under the hood, the plugin uses the Create Skill API to create a skill, which in turn returns a .

The can be viewed by running:

Note: If you have other skills present, the information about those skills will be shown as well.

Add Skill Configuration

In your file, add a new block and paste the and the section into it, as shown below:

For details, check out the full specs for the manifest specs.

With the new information added, let’s update the skill by running:

Note: You can use the option to simulate what the command will do instead of actually doing anything.

Build the Interaction Model

Next up, let’s look at building the Interaction model for our skill. Our skill has a custom intent with a few samples as described by the section. We will also include a few standard Amazon intents namely, Help, Cancel and Stop.

Let’s add the interaction model for our skill in the file, as shown below:

Note: For details, check out the Interaction Model Operations API and the interaction model schema.

Let’s update the skill again by running:

So now we have the configuration and the interaction model for the skill updated and ready to go.

But, wait, we don’t have any code backing up our skill. Let’s do that next.

Lambda functions for the intents

Before we go any further, we need to write our Lambda function handlers for our skill intents in the file.

You can find the code for the lambda function here.

Once we are done with that we need to reference those Lambda functions in the file as shown below:

Note: In this case, the Lambda functions are in the file and the exported function is called .

Now, let’s deploy our Alexa Meetup skill app.

Deploy the Lambda functions

The deploy command will zip up the code, and upload it to AWS. Then, it will map the function handler and give us an endpoint (ARN).

That was easy!!! No clicking through AWS console screens to deploy your Lambda functions.

Now, we need to get ARN for the Lambda function we just deployed. Go to the AWS Lambda service and search for the Lambda function . On the top-right corner of the screen, you will see the ARN.

Grab the ARN and add it to the section of the file, as shown below:

Note: Replace the and with real values for your AWS account.

We made a lot updates to the file. Here is how it looks at the end:

Build the Skill

We will update the skill again by running:

And, finally, we will build the Alexa skill.

Note: You can use the option to simulate what the command will do instead of actually doing it.

View the Interaction Model

Now that the skill has been built, we can see the interaction model, as follows:

Preview the Skill

Let’s see what we have achieved so far. We have created an Alexa skill named MeetupEvents. Let’s preview the skill we created on the Alexa Developer Console.

image
image

After, the skill is built, we can see the Intents populated:

image
image

And, the MeetupIntent utterances are populated as well:

image
image

Test the Skill

First enable testing for the skill. We will be using the Alexa Simulator to test our skill.

We start by typing meetup events, and we hear the welcome response.

Then, we type in my events, and we hear the list of events.

image
image

And, we have a working Alexa Skill that speaks out the latest meetup events.

Logs and Cleanup

If can debug your skill Lambda functions by looking at the logs right from the terminal. And, then you can also cleanup everything after you are done.

Logs

You can view the AWS CloudWatch logs from the terminal by running:

Cleanup

You can delete the Alexa skill, by:

and, you can also cleanup the Lambda functions deploys to AWS, by:

Summary

We started out to build an Alexa skill using serverless. We looked at creating a serverless project for the skill, created the Alexa skill, added an interaction model with intents, and added a lambda function to implement our skill for retrieving meetup events. Then we built and deployed the skill. Last but not least, we previewed and tested the skill using the Alexa Developer Console.

You can access to the full code on my Github repo.

To learn more about building Alexa Skills visit the learn section on the Amazon Alexa site.

If you build something cool, or you have any questions or feedback, please let me know in the comments below.

Originally published at rupakganguly.com

Written by

Thought leader, serverless & cloud technology advocate, problem solver, API Aficionado, enterprise architect with hands-on industry experience. rupakganguly.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store