Skip to content

Instantly share code, notes, and snippets.

@bpgould
Last active April 19, 2023 20:15
Show Gist options
  • Save bpgould/984e8fffca56c56f17420bf1f812791a to your computer and use it in GitHub Desktop.
Save bpgould/984e8fffca56c56f17420bf1f812791a to your computer and use it in GitHub Desktop.
Create a zip archive for python lambdas that includes external dependencies from requirements.txt
#!/bin/bash
# Script to package a Lambda function directory
# into a deployment zip that can be uploaded to S3
# Usage:
# ./package.sh <lambda name/ dir name>
if ! command -v pip >/dev/null 2>&1 || ! command -v python3 >/dev/null 2>&1 || ! command -v zip >/dev/null 2>&1; then
echo "pip, python3, and/or zip are not installed"
exit 1
fi
if [ -z "$1" ]; then
echo "Usage: $0 <lambda name/ dir name>"
exit 1
fi
lambda_dir="$(pwd)/$1"
echo "lambda_dir set to: $lambda_dir"
dependencies_exist="0"
if [[ -f $lambda_dir/requirements.txt ]] || [[ -f $lambda_dir/package.json ]]; then
echo "dependencies found"
dependencies_exist="1"
fi
if [[ "$dependencies_exist" == "true" ]]; then
if [[ -f $lambda_dir/requirements.txt ]]; then
cd "$lambda_dir" || exit
mkdir tmp_package
pip install -r requirements.txt --target ./tmp_package
cd tmp_package || exit
zip -r ../deployment-package.zip .
cd ..
zip deployment-package.zip main.py
rm -rf tmp_package/
else
echo "this script only supports python lambdas currently"
fi
else
cd "$lambda_dir" || exit
zip deployment-package.zip main.py
fi
echo "the deployment package was created"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment