Last active
February 10, 2026 07:26
-
-
Save zmunk/2bc71b940d4a821b59ab073299de5f55 to your computer and use it in GitHub Desktop.
Script for creating an AWS lambda layer for a python library/libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 | |
| $0 python3.11 pydantic pydantic-py311-lambda-layer -- --platform manylinux2014_x86_64 --only-binary=:all: | |
| EOF | |
| ) | |
| if [ -z "$1" ]; then | |
| eecho "please provide python version" | |
| eecho "e.g. '$usage'" | |
| exit 1 | |
| fi | |
| python_version="$1" | |
| shift | |
| # Extract Pip Arguments (everything after --) | |
| # and Script Arguments (libraries and target) | |
| libraries_and_layer_name=() | |
| pip_extra_args=() | |
| is_pip_arg=false | |
| for arg in "$@"; do | |
| if [[ "$arg" == "--" ]]; then | |
| is_pip_arg=true # Everything after this is a pip flag | |
| continue | |
| fi | |
| if [ "$is_pip_arg" = true ]; then | |
| pip_extra_args+=("$arg") | |
| else | |
| libraries_and_layer_name+=("$arg") | |
| fi | |
| done | |
| if [ "${#libraries_and_layer_name[@]}" -lt 2 ]; then | |
| eecho "Error: Please provide library name(s) and a layer name." | |
| eecho "e.g. '$usage'" | |
| exit 1 | |
| fi | |
| # Get last element | |
| layer_name="${libraries_and_layer_name[-1]}" | |
| # get all but last element | |
| libraries=("${libraries_and_layer_name[@]:0:${#libraries_and_layer_name[@]}-1}") | |
| 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 | |
| INSTALL_CMD=($python_version -m pip install $libraries -t $site_packages "${pip_extra_args[@]}") | |
| "${INSTALL_CMD[@]}" # execute installation command | |
| (cd $layer_folder && zip -r ../$zipfile *) | |
| aws lambda publish-layer-version \ | |
| --layer-name "$layer_name" \ | |
| --description "$libraries 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