Adding DynamoDB to Alexa, Testing code changes in 3 seconds and Live debugging

Part 3: Using other AWS services such as DynamoDB

This is an article that continues from the Building an Alexa Skill in 5 minutes with NodeJS or Python without installing any software!, and followed by either the Creation of the AWS Lambda function backend for Alexa with NodeJS or Creation of the AWS Lambda function backend for Alexa with Python articles.

Creating a DynamoDB Table

Although you can use the Sigma IDE to define new AWS resources such as new DynamoDB tables, SQS queues or S3 buckets, etc. while you develop the code and use them, for the scope of this article, let’s see how you can create a new DynamoDB table using the AWS console. Navigate to the DynamoDB Service from your AWS console, and create a new table (e.g. ChineseAnimal) with a column ‘BirthYear’ of type Number as the Partition Key. Then populate a few items as follows with an ‘Animal’ and ‘PersonalityTraits’ attributes for the various years of interest. See this article on the Chinese Zodiac for the information.

Updating the Python Handler

Now go to your custom intent handler (i.e. ChineseZodiacHandler) implementation, and drag the DynamoDB resource from your left-hand palette into the line where you compose the speech text to be returned. Then select ‘Existing Table’ option, and select the table you just created, and select the operation as ‘Get Document’ and provide the Partition key as “@{year}” which means the value of the variable ‘year’.

alexa dynamodb config

Once you click ‘Inject’, the code will be generated for you, and the necessary dependencies added to your project, and the minimum security policy for the operation generated and associated with the Lambda function – all without you having to do any configuration file edits, or manual intervention, etc. Now use the data returned by the DynamoDB operation to construct your speech text.

For Python, this would be as follows, and your code should look as below.

speech_text = "Your animal is a " + data['Item']['Animal']['S']

alexa python handler with dynamodb

Since we introduced a new resource DynamoDB to our Lambda function, we will need to make a Deployment of the function by clicking the ‘Deploy’ button from the toolbar.

Updating the NodeJS Handler

Now go to your custom intent handler (i.e. ChineseZodiacHandler) implementation, and drag the DynamoDB resource from your left-hand palette into the line where you compose the speech text to be returned. Then select ‘Existing Table’ option, and select the table you just created, and select the operation as ‘Get Document’ and provide the Partition key as “@{year}” which means the value of the variable ‘year’.

alexa dynamodb config

Once you click ‘Inject’, the code will be generated for you, and the necessary dependencies added to your project, and the minimum security policy for the operation generated and associated with the Lambda function – all without you having to do any configuration file edits, or manual intervention, etc.

Now use the data returned by the DynamoDB operation to construct your speech text as follows. Note that we will need to update the handle() method to be async, as we use the async DynamoDB calls within it. Also make sure to convert the year to a number, since we have defined it as a Number to DynamoDB. We have also added a catch block to print an error if one is encountered. Your speech text should be updated as follows:

speechText = "Your animal is a " + data.Item.Animal;

alexa nodejs handler dynamodb

Since we introduced a new resource DynamoDB to our Lambda function, we will need to make a Deployment of the function by clicking the ‘Deploy’ button from the toolbar.

Testing code changes in 3 seconds

Once your deployment completes, go to the Alexa Developer Console, and open the skill once more, and ask the question “what is my Chinese zodiac animal for the year two thousand and two”. You should hear back “Your animal is a Horse”. Now let’s enhance our code to include the personality traits as well, by updating the speech text to read as follows.

For Python, update the line to read as follows:

speech_text = "Your animal is a " + data['Item']['Animal']['S'] + '. Wanna know something else? Apparently you are ' + data['Item']['PersonalityTraits']['S']
 
For NodeJS, update the line to read as follows:
 
speechText = "Your animal is a " + data.Item.Animal + '. Wanna know something else? Apparently you are ' + data.Item.PersonalityTraits;

Now click the ‘Quick Deploy’ button on the toolbar next to the ‘Deploy’ button. Your code [only] change will be applied to the live AWS environment in less than 3 seconds! If you see a warning “The project has multiple (non-code) changes, hence a full build is recommended” that’s because you have not committed your code changes into a VCS system, and hence the IDE is unable to safely test the scope of your most recent changes. You can ignore that warning and select to run the Quick Build this time.

alexa quick deploy

You can ask the same question again now from the Alexa Developer Console, and you should hear back “Your animal is a Horse. Wanna know something else? Apparently you are Animated, Active, Energetic”

Local Testing without the Developer Console

If you ask the question for a year for which you do not have a corresponding entry in your DynamoDB table, your Alexa skill will encounter an error. Let’s try this by asking “what is my Chinese zodiac animal for the year nineteen seventy-five”. You should see your Error Handler kicking into action to handle the thrown error, and respond back with “Sorry, your skill encountered an error” as you have configured. If you have the catch block for NodeJS you will instead hear “Sorry I do not know your Chinese animal for the year 1975”.

Now, assuming this is a more complex error that you want to troubleshoot with the Sigma IDE, you can easily copy the JSON input from your Alexa Developer Console, and create a test case in your Sigma IDE as follows. First let’s copy the JSON as follows.

Now go to the Sigma IDE and select ‘Create an event’ from the toolbar, to create a new test case, and paste the JSON to create a new test case which you can name appropriately if you wish.

alexa test case creation

Pressing the Play button next to the test case selected in the drop-down will then execute the test case in the live AWS environment, and show you any output applicable directly at the bottom of your IDE in a new pane titled ‘Sigma Trail’.

alexa cloud watch logs with sigma trail

Thus you do not need to search through Cloud Watch logs to find the log output for the invocation. Furthermore, you can re-execute this test case as often as you wish without having to go through the Alexa Developer Console! You can also search through the trail, double click on an item to show long wrapped lines, and also turn on/off real-time updates of logs, or download the logs, etc.

Live Debugging your Alexa Skill

The live debugging capability is currently available only for Lambda functions implemented in NodeJS, with the Chrome web browser. You may be interested to learn that the Sigma IDE was the very first IDE in the world to allow Lambda functions to be debugged live with breakpoints and step-through debugging, while they executed live on the AWS environment. This capability was introduced in July 2019.

To debug your Lambda function live during its execution, and step through the steps of invoking the DynamoDB call, click the ‘Debug’ icon next to the test case you just created. The very first time you debug your function, the IDE will prompt you to insert one single line into your code, which you should allow. Then it will show the Chrome debugger tools URL for you to connect to your instance.

Copy the Debug URL, open a new tab, and open the link. You will be presented with the Chrome Debugger connected to your Lambda function running live within AWS. You can click on the line where the speechText is defined and initialized to place a break-point, and then click the ‘Go’ button to run the execution until then.

alexa skill debug

Now you can use the ‘Step Over’ button to step through the lines where data is fetched from DynamoDB. Using either the Call Stack or a Watch on variables such as year, data, etc, you will be able to see the variable values as you step through. Since there is an async call to fetch the DynamoDB information, place another break-point on the line where the speechText is computed. When you step through or run until the next break-point, you will stop at the speechText computation line, and notice that the ‘data’ variable does not have any ‘Item’ within it which is our problem!

alexa live debug

If you step through further you will move onto the catch block, and notice how the error response is computed.

Full Tutorial

Here is the complete walk-through of building these applications in both Python and NodeJS with detailed instructions.