Part 2: Creation of the AWS Lambda function backend
For this section, we assume that you have created a free account to use the Sigma IDE and configured your AWS credentials into the IDE. Note that you can register for a free AWS account, and use its Free Tier to develop this exercise, and utilize the Sigma IDE and AWS to build and host many other useful applications – all for free! Please note that this post is a continuation of Building an Alexa Skill in 5 minutes with NodeJS or Python without installing any software!
Here is the 5 minute video for those who are impatient! We finish the application in less than 5 minutes, including the VUI creation and waiting time for the AWS deployment of close to a minute, and use the last minute to show you how code changes can be applied to live Alexa in less than 3 seconds.
Creating a new Project
First, create a new project and name it ‘ChineseZodiacAnimal’. As you can see the Base cloud Platform is set to AWS which we will use for this Alexa skill, but the Sigma IDE can also build GCP projects. For the Python implementation, we will set the default Primary Language as Python. Click ‘Create’
Once you are presented with the basic editor view, go to the ‘Utility functions’ tab from the left-hand pane where you can see the Alexa specific functions as follows.
The purpose of the functions are as follows:
- Alexa Lambda Scaffolding – Drag to create the basic Lambda function definition for an Alexa skill in the selected language, and include the basic Alexa skill kit (ASK) libraries as dependencies to the project
- Custom Intent Handler – Drag to create a custom intent handler
- Launch Request Handler, Error Handler, Session Ended Request Handler, and Built-in Intent Handler allows you to generate the basic handlers for the said actions.
First, delete the existing code in the code pane, and drag the Alexa Lambda Scaffolding icon to the code pane. The basic Lambda function code is generated instantly, and the dependencies will show that the ask-sdk-core version 1.13.0 for python has been included into the project as a dependency as well.
The minimum Lambda handler implementation will require a Launch Request Handler and an Error Handler, which we will drag into the code pane next. Once I drag a Launch Request Handler to the end of the current function definition, the following panel will popup, asking me for the name of the handler, and the response to be returned to the user when the skill is launched. We will customize the welcome message to ‘Welcome to the Chinese Zodiac Animal skill’ and click ‘Inject’.
This step will inject the code for the handler as follows, which you can further extend or customize as necessary!
Similarly, I will drag an Error Handler into the code pane, and leaving the defaults for the handler name and response. Then I will drag a Custom Intent Handler into the pane, and name the intent ‘ChineseZodiac’ which will map exactly to the same intent name I gave within my Alexa Developer Console for the Voice User Interface. Note that with Python, the code for the custom handlers need to be defined before they are used in the main Lambda function handler below.
I will delete the undefined handlers from my main Lambda function handler, and insert the name of the custom intent handler and make the code appear as follows, and then drag ‘Alexa Skill’ from the AWS resource panel into the handler function definition line as follows.
This will prompt for the Alexa Skill ID which can trigger this Lambda function, and thus, I will paste the Skill ID which I copied from the Alexa Developer Console -> Endpoints page here as follows.
When I click ‘Inject’, the Lambda function line will have a green color icon set along its line number indicating the Alexa skill trigger.
To deploy the Lambda function into AWS, click the ‘Deploy’ button on the toolbar. The first deployment will take about a minute, and will create the lambda function, necessary security policies, and roles, etc and perform all the Cloud Formation magic, without you having to edit any configuration files, or perform CLI commands, etc!
Once the deployment completes, the ARN of the Lambda function is shown to you, which you will need to copy and paste into the Endpoints section in your Alexa Developer Console.
Now you are ready to navigate to the ‘Test’ tab in your Alexa Developer Console, set the Skill testing mode to ‘Development’ and input ‘open chinese zodiac animal’. Your skill will be successfully invoked, and the response you set returned to you!
Now if you ask ‘what’s the Chinese zodiac animal for the year two thousand and twelve’ you will receive the response ‘This is my custom intent handler for the year 2012’ which shows that your custom intent handler is also working, and correctly picked up the slot value you passed in.
If you’ve succeeded in following up the steps until now, you will find the full video much more helpful in understanding how you can now test the skill, extend it to get data from a DynamoDB table.
Follow the Adding DynamoDB lookup to an Alexa Skill Testing and debugging article to continue this tutorial walk-through.