Amazon Lambda Deployment Workflow For Alexa Skill
Set Up An Alias
One thing I really struggled with was the fact that you can’t set a trigger on the current version of a lambda function that you’re editing through the web interface (called $LATEST
). However it turns out that you can create an alias that points to $LATEST
rather than a specific version number, so the alias will always be whatever version you’re on.
Now go ahead and create a Trigger for your Lambda function which is the “Alexa Skills Kit”. With this in place, you can create (or edit) an Alexa skill through the Alexa Developer Console and configure the location of the function to the arn including the alias you created (mine is called “prod”).
At this point you can create and publish new versions of code without having to recreate the trigger and update the skill itself, progress!
Edit Code Locally, Then Deploy
Editing code in a web interface is never a good way to do anything more than a trivial example and especially for this vim user, it got very tedious very fast! It’s possible to deploy code to Amazon Lambda by zipping up your file, putting it onto Amazon’s S3 storage platform, and then making an API call to notify Lambda to pick up the new version – which is to say, that this is all scriptable!
To get this to work, you’ll need Amazon’s CLI tool installed on your machine (or wherever you want to deploy code from – would also make a lot of sense on a build server). Get that installed and check it with aws --version
, which should give you some sane output.
Next you need a script that will:
- create a zip file containing the `index.js` file that your function lives in
- upload the zip file to S3
- get lambda to use the new code
My script looks like this:
#!/bin/bash rm -f lorna.zip cd src zip ../lorna.zip index.js cd .. aws s3 cp lorna.zip s3://alexa-lorna/ aws lambda update-function-code --function-name AlexaPing --s3-bucket alexa-lorna --s3-key lorna.zip
This just deletes the previous zip file (you could delete it at the end of the script but not clearing up build artifacts makes debugging easier!), creates a new one from the directory level that the zip file should unzip its paths relative to, and puts the index.js
into it. We upload to an s3 bucket called alex-alorna
. Finally, there’s a command to tell lambda to use the new code for the function named AlexaPing
and which file to use.
With all this in place, I can edit locally (you could rig up a little hacky script to call it locally as well) and then run the script above to deploy the new version. Hopefully one or more of the ingredients here will prove useful to someone in the future, it definitely felt harder to figure out than it needed to be!
Pingback: Alexa, When’s the Bus? | LornaJane
Note that you can also supply just the zip file, without s3 (I came across this working on a later demo). So the update call has –zip-file fileb://hello.zip pointing to a local zip file
Hello,
I like your article! I am working with a group on github that involves controlling directv receivers with alexa. To get this to work, the user has to update the wan ip in the aws lambda function. for users that have dynamic wan’s this changes from time to time. So what I was looking for was to possibly set an alexa skill that would update a variable in an s3 bucket, and then have the s3 bucket zip up the new index with a partnering file and update the lambda function.
From your experience, would this be something that is doable?
flow would look something like this:
alexa wan update skill–>s3 bucket–>update aws lambda function.
I invite you to take a look at our setup if you want. It is geared to help people who have physical impairments and iot fans.
https://github.com/bklavet/Echo-skill-to-control-Directv