Skip to content

Instantly share code, notes, and snippets.

@mikeblum
Created July 3, 2016 04:00
Show Gist options
  • Save mikeblum/260e843986c84c9c9c0c2f1bea82188a to your computer and use it in GitHub Desktop.
Save mikeblum/260e843986c84c9c9c0c2f1bea82188a to your computer and use it in GitHub Desktop.
Deploy Python Lambda fucntions to AWS
#!/bin/bash
# expects the lambda function .py file to be in the same directory as this script.
# based off of Amazon's official documentation:
# http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-python
# get the lambda function
lambda_func_file=$1
lambda_func="${lambda_func_file%.*}"
# exit if no file specified
[[ -z "$1" ]] && { echo "Lambda function is empty" ; exit 1; }
# generate a deployment timestamp
timestamp=$(date +%Y-%m-%d-%H:%M:%S)
# create a deployments directory if one doesn't already exist
mkdir -p ${PWD}/deployments
zip_file="lambda-func-$lambda_func-$timestamp.zip"
zip_path=${PWD}/deployments/$zip_file
# ignore folder path to site-packages but preserve the structure inside of site-packages
pushd $VIRTUAL_ENV/lib/python2.7/site-packages/
zip -r9 $zip_path .
popd
pushd $VIRTUAL_ENV/lib64/python2.7/site-packages/
zip -r9 $zip_path .
popd
# add Python code
zip -9 $zip_path $lambda_func_file
# echo back the deployment ZIP for further processing
echo 'deployment ZIP: ' $zip_path
# deploy to AWS Lambda
aws lambda update-function-code --function-name $lambda_func --zip-file fileb://$zip_path --publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment