How to build an Alexa skill

How to build your first skill with Alexa and AWS Lambda

Alexa is Amazon's voice virtual assistant and the brain behind of the devices like Amazon Echo, Echo Dot, Tap, Fire TV and Echo Show. Basically, Alexa is to Amazon what “Siri” is to Apple, “Hey Google” to Google and "Cortana" to Microsoft. This is how Amazon describes the Alexa  Service on their Alexa developer page:

“The Alexa Voice Service (AVS) is Amazon’s intelligent voice recognition and natural language understanding service that allows you to voice-enable any connected device that has a microphone and speaker.”

What can Alexa do?

Alexa will play music, tell you a joke, order a ride, read books from your Kindle, provide information, read news and sports scores, order pizza, get recipe ideas, tell you the weather, control your smart home, order product for you on Amazon, check stock price, find out what’s new on Netflix etc.

Alexa skills

Alexa provides capabilities, or skills, that enable customers to create a more personalized experience. Skills are voice-driven capabilities that enhance and personalize your Alexa devices. There are now more than 25,000 skills from companies like Spotify, Twitch, Uber, Starbucks, Fox news as well as skills from other designers and developers. To enable Alexa skills:

  • Go to the Alexa menu, and select "Skills". Or, go to the Alexa Skills store on the Amazon website: https://amazon.co.uk/skills.
  • When you find a skill you want to use, select it to open the skill detail page.
  • Select the "Enable Skil"l option, or ask Alexa to open the skill.

Create custom Alexa skill and Lambda function

  • Open Amazon Developer Portal and click "Sign In". (If you don't have an account, you should create a new one for free.)
  • Next, set up an Alexa skill by clicking on "Add a new Skill" button.
  • In the Alexa Skills Console select the "Create Skill".
  • Add a name for your skill (e.g alexa-random-number). That would be the name that is shown in the Alexa Skills Store, and the name that your users will see. 
  • Press Next.
  • Select the Custom model to add it to your skills.
  •  Click on "Create Skill".
  • Build the Interaction model for your skill.

{

"intents": [

{

"intent": "GiveRandomNumber",

"slots": [

{

"name": "UpperLimit",

"type": "AMAZON.NUMBER"

}

]

}

]

}

Your interaction model should build successfully after that proceed to the next step.

So far we have built the Voice User Interface(VUI) for our Alexa skill. Next, we will create a lambda function. You can read more about lambdas here.

  • Go to http://aws.amazon.com and sign in to the console.
  • Click "Services" at the top of the screen, and find Lambda in the list of services.
  • Select your AWS region. Note that AWS Lambda works with the Alexa Skills Kit in few regions:
  1. US East (N. Virginia).
  2. US West (Oregon).
  3. Asia Pacific (Tokyo).
  4. EU (Ireland).
  • Select "Serverless Application Repository".
  • Find the application repository named "alexa-random-number". You can find it using the search box.
  • Click on the repository. This repository will create the Lambda function.
  • Next, add Alexa Skills Kit as it's trigger, and sets up an IAM role.
  • Next, add the NodeJS code.

'use strict';


/* Returns a random integer */

const getRandomInt = (min, max) => Math.floor(Math.random() * ((max - min) + 1)) + min;

module.exports.randomNumber = (event, context, callback) => {

const upperLimit = event.request.intent.slots.UpperLimit.value || 100;

const number = getRandomInt(0, upperLimit);

const response = {

version: '1.0',

response: {

outputSpeech: {

type: 'PlainText',

text: `Your random number is ${number}`,

},

shouldEndSession: false,

},

};

callback(null, response);

}

  • Click "Deploy" lambda.
  • Wait till status change to "CREATE_COMPLETE".
  • Click the "Test App" button to go to the Lambda console.
  • Click on the function that was just created.
  • The Amazon Resource Name (ARN) a unique identifier for this function should be visible. 
  • Copy the ARN. 

Connecting  VUI To Your Lambda Function

First, we have created a voice user interface for the intents. Then we have created a Lambda function that contains logic for the skill. Next, we will need to connect those two together.

  • Go back to the Amazon Developer Portal, select skill from the list and select the Endpoint tab on the left side navigation panel.
  • Copy the "AWS Lambda ARN" option. 
  • Paste it into the Default Region text box.
  • Click the "Save Endpoints" button at the top of the main panel.

Test your skill

  • Open the Test Panel by selecting the "Test link" from the top navigation menu.
  • Click on the "Test is enabled" to enable testing for this skill slider.
  • You should invoke your skill in the Alexa simulator to validate if it is working as expected.
  • Ensure your skill works the way that you designed it to.

If this skill is not working as expected, you can check the JSON to see exactly what Alexa is sending and receiving from the endpoint. 

When you decide your skill to be made public, and you are sure that it fulfills the requirements on the certification checklist, submit your skill for certification. Once your skill is published, Amazon customers can see it in the Alexa App and choose to use it.

After that you can still continue working on your skill, fixing bugs etc. For further recommendations check  Launch Your Skill: Revise and Update Your Skill after Publication