This is part 3 of a series on using AWS Lambda to build an Alexa Skill. Refer also to the overview of the server-less app (part 1), and configuring a custom Alexa Skill (part 2).
I chose to use a Lambda function for my skill's "backend", rather than using a traditional server. Lambda is marketed as server-less compute, meaning code is only spun up and executed upon a request, instead of a server always being "on" and listening for requests.
There are 3 main advantages for this:
However there were also a couple of drawbacks, namely:
Your lambda function needs an entry point or "handler" whenever it receives a request. By default, this is set to lambda_function.lambda_handler although this can be set in the configuration section in the Lambda web console.
What this means is your lambda function expects a file called lambda_function.py, and it has a function called lambda_handler. This is the place to handle routing of the intents we discussed in configuring your Alexa skill. Note that it always receives an event and context object when requested.
For example:
# lambda_function.py
def lambda_handler(event, context):
print("Received event %s" % event)
# Check your lambda function is only being called by your Alexa skill
if event['session']['application']['applicationId'] != "my-alexa-skill-id":
raise ValueError("Invalid Application ID")
if event['request']['type'] == "IntentRequest":
intent = event['request']['intent']
intent_name = intent['name']
# Dispatch to your skill's intent handlers
if intent_name == "IntentA":
# ... do something
elif intent == "IntentB":
# ... do something else
# Your lambda function can also serve HTML if accessed via an API Gateway
return {
'statusCode': '200',
'body': "<html><h1>Welcome</h1></html>",
'headers': "{'Content-Type': 'text/html'}"
}
Notice the code blocks above relating to intents. These are specific to Alexa skills, and routes any voice commands from Alexa into the rest of your app.
This lambda_function.py file can either be:
Assuming you are using other python packages and need to upload a zip file rather than only using a single file, you will need to create a Deployment package for your lambda function.
To do this, you must:
Note that Boto3 is already included by default in all lambda functions, so just import it - there is no need to include in your zip file. Boto is the Python package for interacting with AWS services like DynamoDB.
Once your code has been uploaded into your lambda function, you can configure other services to make requests to this lambda. These are known as "triggers" for your lambda function, and are set in the web console.
Triggers could be:
Once your triggers are setup, your lambda function gets requested with a bunch of environment variables set automatically. You can include something like this in your code to see the full list:
import os
print(os.environ)
The most useful ones though are environment variables like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION - these are used by Boto3 if you use any other AWS services like DynamoDB, S3, etc in your lambda function.
Another issue to be aware of is that Lambda does not currently handle x-www-form-urlencoded POST params (this appears to be a known issue at the time of writing). These POST params are passed in the event['body'] as a url-encoded string, eg "email=myemail%40gmail.com&otherparam=23".
To parse these, I usually include a small helper function which parses the string into a standard Python dictionary.
import urllib2
def parse_post(event):
post = {}
body = event.get('body', None)
if not body:
return post
parsed = urllib2.unquote(body)
key_pairs = parsed.split("&")
for kp in key_pairs:
pairs = kp.split("=")
if len(pairs) == 2:
post[pairs[0]] = pairs[1]
return post
Your lambda function can send requests to other services, store data in a database, and anything else you like (just like a normal server). Now with your Lambda function receiving requests from your Alexa skill, you have everything you need to use voice commands for your app. If you have been following part 1 and part 2 of this series, just follow the submission guidelines to tidy up your Alexa skill and your Skill will be available to all Alexa users.
Good luck!