by

NPM Deploy Script for Alexa skills and AWS Lambda

As a follow up to Alexa Skills and AWS Lambda Aliasing wrote up a little NPM script to get some folks started.

# Create a unique filename based upon the current time
timestamp=`date +%s`
echo $timestamp
filename="myProject.$timestamp.zip"

# We don't want to upload all the modules required to run our test harness' so prune out devDependencies
npm prune --production
cd ../src
zip -9 -x "test/*" "coverage/*" -X -r ../dist/$filename *
cd ../dist

# Upload the zip to AWS to update the Lambda Function ($latest)
aws lambda update-function-code --function-name myProject --profile myProfile --zip-file fileb://$filename

# Publish a new version and grab that version number
# (this requires jq https://stedolan.github.io/jq/download/)
version=`aws lambda publish-version --function-name myProject --profile myProfile | jq -r .Version`

#Add the Alex-Skill trigger to just published version
aws lambda add-permission --function-name myProject:$version --profile myProfile --statement-id $timestamp --action lambda:invokeFunction --principal alexa-appkit.amazon.com --region us-east-1

#tag the repo with the lambda version
git tag -a v$npm_package_version -m "lambda version $version"

git push --tags
cd ../src

# Add our devDependency modules back in to resume development
npm install
Assumes a couple of things

You have aws-cli installed

You have aws-cli credentials and profiles setup

You have jq installed

Project file structure:
{project}
{project}/bin (location of this deploy.sh file)
{project}/dist (where you will store your zip files)
{project}/src (where your lambda code lives)
{project}/src/package.json
{project}/sr/covergage (where code coverage reports end up for us)
{project}/src/test (where you keep your tdd scripts)

The code above goes into {project}/bin/deploy.sh

For your package.json:

"scripts": {
"deploy": "sh ../bin/deploy.sh"
}

An npm run deploy will package and upload your code to lambda, set a trigger, version it and tag your repo. Enjoy.