My Alexa Skill with Storage

Liz Rice
HackerNoon.com
4 min readJan 2, 2017

--

I recently wrote about my first foray into writing an Alexa custom skill. My next step is to extend my very simple skill to have some storage.

At the moment, all my custom skill does is read a list of people who are currently in the house — and that list is hardcoded. I want to update it so that it reads the list of people’s names from a database.

What storage?

My natural inclination would be to store my information in a PostgreSQL database running under RDS. But I’m pretty sure I’m past my free tier year on AWS, so I’m attracted to DynamoDB as a small amount of usage is free indefinitely. I’ve never used Dynamo before, but now seems as good a time to learn as any!

Create a table

It’s straightforward to set up my table of names and add a couple of entries.

Python code to read the names out of the table

I found some sample code for using DynamoDB with Python in the Amazon documentation. Using that as a starting point I wrote the following to read the list of names from my DynamoDB table and return it as a string.

The sample code specifies an endpoint on localhost as one of the parameters in the boto3.resource call because it’s using an instance of Dynamo running locally, but you simply omit the endpoint if you’re running DynamoDB on AWS. You do need to get the region_name parameter right.

I want my spoken list of names to sound natural, so I am separating them with commas and splitting the last two with the word “and”. I updated the lambda function I wrote a couple of days ago to use this list of names instead of the hardcoded version I had previously.

Permissions

My Lambda function needs to be allowed to access the Dynamo table. To allow this I’ve added the AmazonDynamoDBFullAccess policy to the set of managed policies for the IAM role my Lambda function is using (which I had previously created and called myAlexaRole).

Testing the lambda function

As before, you can simply hit the Test button to see if the function behaves as expected.

Connecting to Alexa

I lied earlier — or at least I omitted to mention that I created a new Lambda function called myStorageLambda rather than editing the original version. It’s possible to publish new versions of the same Lambda function, but I’m going to skip over that for now.

I don’t need anything new from my Alexa skill, but I simply need to update it to point to myStorageLambda by configuring it to use the ARN for that function.

It works!

While I was writing this…

…we had the TV on in the background, and for the first time Alexa mistook something they said as as instruction for her. I have no idea what they actually said, but I’m pretty sure it wasn’t what she heard…

What’s next?

Next up, I want to change the lambda function and add some more intents so that we can let Alexa know when people enter or leave the house, without modifying the Dynamo table directly.

I’m intending to post the code to GitHub but I haven’t got around to that yet. Would you find it useful? Let me know in the responses.

If you liked this article, please hit the Recommend button so that other people might find it more easily and write their own Alexa skills or incorporate DynamoDB into their Lambda functions.

Next up — here’s Part 3 of my Alexa journey, where I add conversational intents

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--

Liz Rice
HackerNoon.com

Containers / eBPF / security / open source @isovalent @ciliumproject / cycling / music @insidernine