“Alexa, terminate all my untagged instances.”
Imagine being able to do common AWS administrative tasks just by using your voice. At a recent
Utah-AWS Meetup we demonstrated how, in a matter of a few minutes, you could be automating your own AWS Administration tasks using an Amazon Echo and AWS. The goal of this post is to show you how easy it is to use AWS Lambda to create new Alexa skills. We’ll walk you through the creation of a Lambda function, creating all the resources necessary for an Alexa skill, and finally how to deploy and test it on your own Amazon Echo.
All the source code for this blog post can be found at
https://github.com/1Strategy/alexa-aws-administration.
Lambda Function
The Lambda function is organized into “intent” handlers (with associated helper methods) with some boilerplate code for selecting the correct handler to run based on what the Alexa service determines the user is trying to do.
The onIntent function maps the intent that Alexa believes the user is trying to invoke, and executes the corresponding function to handle that intent. Here’s a brief summary of the intents that are currently supported:
Welcome Response (getWelcomeResponse)
This function provides an introduction when a user launches the service on their Echo. It provides instructions on available commands and suggests an initial command to use, in this case “Select a region to use by saying, set the region to Virginia.”
Set Region (setRegion)
When this intent is triggered, the user will have specified a desired region to work in and that region will be set in session (to be used in later requests).
Get Region (getRegionFromSession)
Will echo back which region is currently in session.
Get Instance Count (getInstanceCount)
Returns the number of running instances in the region that is currently in session.
Terminate Untagged Instances (terminateUntaggedInstances)
Will filter your instances and find those that are untagged (including instances with a blank name tag), and then terminate those instances. For more information on the code that does the filtering and terminating, please see
http://www.1strategy.com/blog/use-aws-lambda-terminate-untagged-ec2-instances/.
Preparing the Lambda Function to Be an Alexa Skill
Make sure to create the Lambda function in the “N. Virginia (us-east-1)” region, as that it is the only region that supports receiving events from Alexa Skills Kit. After saving your Lambda function, make note of the Lambda’s ARN, we’ll need that when we setup the Alexa Skill.
In addition, we’ll need the following items:
intents.json
The Alexa Skills Kit needs to know what intents are available in your new skill. This is done by providing a JSON document that describes each of the intents. You may notice that the SetRegionIntent has an additional “slots” property. You can think of a “slot” as a variable that the user provides. In this case, we want the user to specify a region variable, so we know which AWS region to operate against. You’ll also notice that it references a “LIST_OF_REGIONS” type. We’re going to define that type now.
LIST_OF_REGIONS
We want to limit the number of choices that can be used for the region slot (variable). We don’t want the user to be able to specify a location that doesn’t exist, so we limit what is accepted by supplying a list of valid words for this slot. Currently, we’re limiting the options for regions to “Virginia” and “Oregon”. To support more regions, we would simply add additional lines to this file and then ensure that the getRegionIdentifier function in our Lambda knows how to map the additional choices.
Sample Utterances
The Alexa Skills Kit uses machine learning to determine what the user is intending to do. We feed example phrases into the Alexa Skills Kit as input, including the desired intent to be triggered. We supply these examples by defining “sample utterances.” We’re supplying just a few examples of things users could say, and what intent should triggered. Note the use of the “{Region}” placeholder in some of the SetRegionIntent phrases. This is a link to the slot discussed earlier.
Testing your Alexa Skill
Now that we have our Lambda and the resources that the Alexa Skills Kit needs, let’s put it all together and test it out. Start by heading to http://developer.amazon.com. If you haven’t registered for an Amazon Developer Account, do so now, it’s free! In order to test on your own Echo, make sure the email address you’re using for a developer account matches that email address to which your Echo is registered.
Once logged in to the Amazon Developer Console, navigate to “Apps and Services” -> “Alexa”. Click the “Add a New Skill” button.
Skill Information
Fill in the required fields. The “invocation name” will be the identifier you use to open up this skill. For example, I used “admin tools”. In order to use the Alexa Skill, I would say, “Alexa, open admin tools.” Make sure to use the ARN captured above after you created your Lambda function.
Tip: I wasn’t able to find any information about this in the Alexa Skills Kit documentation but the skill never seemed to work when I included “AWS” or “Amazon” in the invocation name.
Interaction Model
This is where we tell the Alexa Skills Kit what our skill can do. In the “Intent Schema” box, paste the contents of the
intents.json file. Next, add a new slot type, giving the new slot a type name of “LIST_OF_REGIONS” and paste the contents of
LIST_OF_REGIONS.txt into the value field. Paste the contents of
sample_utterances.txt into the “Sample Utterances” field and click next.
Test
You’ll be presented with a screen that you can use to test example inputs and see what the skill returns. It can take a few seconds for the Alexa Skills Kit to build the model for use on your Echo. As soon as you see three green checkmarks on the left side, you should be good to test it out on your Echo!
Congratulations, you just took your first steps into the exciting new world of VoiceOps!
Obviously, there are many more features to add to this skill. Please use this as a starting point to build bigger and better skills. We’re excited to see what you come up with!
Demo
See a video of
this project in action on YouTube.