Skip to content

Instantly share code, notes, and snippets.

@zmunk
Last active November 8, 2025 08:22
Show Gist options
  • Select an option

  • Save zmunk/2bc71b940d4a821b59ab073299de5f55 to your computer and use it in GitHub Desktop.

Select an option

Save zmunk/2bc71b940d4a821b59ab073299de5f55 to your computer and use it in GitHub Desktop.
Script for creating an AWS lambda layer for a python library/libraries
#!/usr/bin/env bash
# To run this script directly from this gist, run the following command. Make sure to replace
# the arguments with your desired arguments.
#
# curl https://gist.githubusercontent.com/zmunk/2bc71b940d4a821b59ab073299de5f55/raw/create-lambda-layer.sh \
# | bash -s python3.11 schematichq pydantic pydantic_core schematic-py311-lambda-layer
# Exit the script immediately if any command returns a non-zero (failure) exit code.
set -e
# Echo red bold text to stderr
function eecho { echo -e "\e[1;31m$@\e[0m" 1>&2; }
# check if aws cli is configured
if ! aws sts get-caller-identity >/dev/null ; then
eecho "please configure aws cli"
exit 1
fi
usage=$(cat << EOF
$0 python3.11 redis redis-py311-lambda-layer
$0 python3.11 schematichq pydantic pydantic_core schematic-py311-lambda-layer
EOF
)
if [ -z "$1" ]; then
eecho "please provide python version"
eecho "e.g. '$usage'"
exit 1
fi
python_version="$1"
if [ "$#" -lt 3 ]; then
eecho "please provide library name(s) and layer name"
eecho "e.g. '$usage'"
exit 1
fi
# Get all arguments from position 2 to second-to-last
library="${@:2:$#-2}"
# Get the last argument
layer_name="${!#}"
cache=.aws_cache
layer_folder="$cache/lambda-layer"
site_packages="$layer_folder/python/lib/$python_version/site-packages"
zipfile=lambda-layer.zip
mkdir -p $site_packages
$python_version -m pip install $library -t $site_packages
(cd $layer_folder && zip -r ../$zipfile *)
aws lambda publish-layer-version \
--layer-name "$layer_name" \
--description "$library lambda layer for $python_version" \
--license-info "MIT" \
--zip-file "fileb://$cache/$zipfile" \
--compatible-runtimes "$python_version" \
--query "LayerVersionArn" \
--output text
rm -r $layer_folder
rm $cache/$zipfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment