For my most recent project, I wanted to have a way to control my RaspberryPi from my Amazon Echo. I already figured out a way to connect to my RaspberryPi from AWS in a secure way in my last post. Now it was time to play with the other end: writing an Alexa Skill. As a first step towards that direction, I wanted a catch-all Alexa skill that calls an AWS Lambda. This post outlines how I got an AWS Lambda triggered by catch-all Alexa Skill.
AWS Setup
The first thing needed to do this whole project is an AWS account. In order to create one, follow the instruction on the AWS Portal.
At the time of this writing, AWS Lambda has a free tier allowing 1,000,000 free requests per month and up to 3.2 million seconds of compute time per month. There is no reason why this should not be enough for this project. But do watch out: you can easily occur costs if you add other services.
Once you created your account, you will need to generate an access key for your user. It is a bad plan to create an access key for your root user. You should look into how to secure your AWS account but for the sake of this quick project, following this guide will give you what you need.
Alexa Developer Console Setup
In the Developer Console, you will create a new Skill. That new skill will use a custom model and backend resources that you will provision yourself. No need to select any template, we will be doing all the work ourselves.
The Alexa Skill
I won’t go over everything that needs to be done to configure the Skill. The most important parts I could find to achieve what I wanted were:
- Create a new Intent; in this Intent, add one slot of type AMAZON.SearchQuery
- In the Tools / Accounts Linking section, you will need to disable account linking (first option)
Every time you do changes, remember to hit the Build button. This will validate your setup. You can also go into the Certification section. This will allow you to run validations on your configuration and give pointers on how to fix issues.
The AWS Lambda
The next step is to create your AWS Lambda. In order to do so, simply go to the AWS Lambda home page and hit Create Function. We will be writing our function from scratch, using a Python 3.7 runtime.
The code we will be using for this lambda resides in this repository. It does not do much: it outputs any slots it could find to the logs of your lambda. But it can at least show that the linking worked correctly.
Deploying the code can be done directly through the Lambda UI or using the publish.sh script in the above repository. Once deployed you can use the Test button directly in the Lambda UI in the AWS portal to trigger a run of the code. You will need to enter the content of the lambda_test.json file when asked for a test configuration.
Linking the Skill to the Lambda
In order to link everything, you will need two pieces of information:
- The ARN of the Lambda you created
You can get this information by going to the AWS Portal and selecting your Lambda, the ARN will be at the top right - Your Skill ID
This information you can get from the Amazon Developer portal, by going into your skill and into the endpoint section
The linking needs to be done in both direction:
- In the Amazon Developer portal, in the endpoint section of your skill, you will need to enter the Lambda ARN into the default region field
- In the AWS portal, after selecting your Lambda, you will need to add a new trigger of type Alexa Skills Kit and enter the Skill ID of your skill
Testing Everything
There are multiple ways to test this setup:
- Testing using an Echo device
- Use the Amazon Alexa app on your smartphone
- Use the test UI in the Amazon Developer portal
I strongly suggest using the Amazon Developer portal since this removes the ambiguity of speech-to-text. In order to access this UI, simply go onto the Amazon Developer portal, select your skill and go into the Test section on top. There you will be able to enter text directly into the simulator.