In this demo, We’ll go over how to write Alexa skill data to DynamoDB with AWS Lambda.
The skill, we’re going to build is an expense tracking assistant called “My expenses”. The user can ask to add an expense record and state the amount and the category of the expense. The record will be then stored in AWS DynamoDB and associated it with an userId.
The following video gives a quick demo of what the finished product will be:
DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It requires no schema and can be used as a key-value or document-based store.
Skill will also require either an AWS Lambda ARN or a publicly available HTTP endpoint as a backend to process the logic behind the skill. We’ll be going with Lambda ARN, So we don’t need to run a server, and connect the skill via HTTP.
Creating a skill on the Amazon Developer Portal
- Go to Alexa developer console https://developer.amazon.com/alexa/ and add a new skill named “My expenses”.
- In the next screen “Choose a model to add to your skill” and pick the “Custom” option.
- Create the Interaction model. The quickest way to configure the interaction model is through the “JSON editor” in developer console. Copy and paste the code below in the editor and save the model. This will gives you:
— invocation name: my expenses
— Intent AddExpense
— Sample utterances.
— Slots - Amount, Category
If you’re new to Alexa, please read more about Intents, Utterances, and Slots. So You’ll be able to understand the basics of Alexa.
Create Lambda function
We can write lambda function in any of these programing languages — (as of this writing ) Node.js, Python, Java (Java 8 compatible), and C# (.NET Core) and Go are supported. You can deploy the function either using AWS CLI or from AWS management console and configure Alexa to invoke the lambda.
You can also use a tool like “Serverless” — write your code, configure events to trigger your functions, and then deploy & run those functions to your cloud provider via the CLI provided by Serverless. Getting started with Serverless microservices with AWS Lambda.
For the purpose of this demo, I’ll be creating a serverless project with nodejs:
$ serverless create --template aws-nodejsServerless: Generating boilerplate...`
Open handler.js and update the code
Deploy the lambda function with
serverless deploy command and find its ARN. Go back to your Alexa developer console -> build -> Endpoint and update the AWS Lambda ARN.
Also don’t forget to update the
alexa.appId = “amzn1.ask.skill.XXXX” value in your lambda function. (line 10, handler.js)
This will also create your Dynamodb table with the required permissions. You can learn more about serverless dyanamodb templating here. Behind the scene serveless.yml will be transformed in to a single Cloudformation stack, you can see them from AWS console -> Cloudformation.
AWS CLI credentials are stored in INI format in
~/.aws/credentials on a separate aws profile named
serverless and serverless will pick the profile credentials during the deployment.
prod: serverless$cat ~/.aws/credentials
aws_access_key_id = XXXX
aws_secret_access_key = XXXX
Testing on your Echo
Now your code is ready and deployed, go try your skill out!
You can say “Alexa, open My Expenses” or just type in the words to fire your skill from Alexa developer console -> Test.
I hope you found this tutorial helpful. Please feel free to contact me, If you get stuck on anything. You can find the code at github.
Happy coding with Alexa ❤