I mentioned in my post about creating a new skill for Alexa that I found the workflow quite clunky. Having tried a bit more and bugged a few people to find out how they hadn't killed anyone trying to develop with a workflow like this, I have some tips to share!
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!