Skip to content

Instantly share code, notes, and snippets.

@lucafrost
Last active January 11, 2024 15:53
Show Gist options
  • Save lucafrost/6f731ccaeb3e596aeab0f18fc84711ac to your computer and use it in GitHub Desktop.
Save lucafrost/6f731ccaeb3e596aeab0f18fc84711ac to your computer and use it in GitHub Desktop.
Publish AWS Lambda Layer for Python Package with 1 Command ⚡️
#!/bin/bash
# EXAMPLE USAGE: ./publish-lambda-layer.sh numpy
# check if a package name was provided
if [ -z "$1" ]; then
echo "Please provide the name of the Python package you want to publish to Lambda as the first argument to this script."
exit 1
fi
# install the AWS CLI if not already installed
if ! [ -x "$(command -v aws)" ]; then
echo "Installing the AWS CLI..."
pip3 install awscli --upgrade
fi
# create a deployment package
echo "Creating deployment package..."
package_name="$1"
package_zip="$package_name.zip"
mkdir $package_name
mkdir $package_name/python
pip3 install --target ./$package_name/python $package_name
cd $package_name
zip -r ../$package_name.zip .
cd ..
# get the package version
package_version=$(pip3 freeze | grep "$package_name==")
# publish the deployment package as a Lambda layer
echo "Publishing deployment package as a Lambda layer..."
aws lambda publish-layer-version \
--layer-name "$package_name" \
--zip-file fileb://"$package_zip" \
--description "$package_name v$package_version" \
--compatible-runtimes python3.10
# clean up
rm -rf "$package_name" "$package_zip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment