Skip to content

Instantly share code, notes, and snippets.

@dryan
Created April 23, 2024 18:46
Show Gist options
  • Save dryan/e897099db74c8ae1952a5754c7eb4641 to your computer and use it in GitHub Desktop.
Save dryan/e897099db74c8ae1952a5754c7eb4641 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-v | --version)
shift
PY_VERSIONS+=($1)
shift
;;
-* | --*)
echo "Unknown option: $1"
exit 1
;;
*)
POSITIONAL_ARGS+=($1)
shift
;;
esac
done
if [ ${#PY_VERSIONS[@]} -eq 0 ]; then
PY_VERSIONS=(3.12)
fi
for PACKAGE in "${POSITIONAL_ARGS[@]}"; do
if [ -z "$PACKAGE" ]; then
echo "No package specified"
exit 1
else
echo "Building layer for package $PACKAGE with python versions: ${PY_VERSIONS[@]}"
mkdir -p "./$PACKAGE"
# for each python version, create a virtual environment, install the package and zip it
for PY_VERSION in "${PY_VERSIONS[@]}"; do
PY_DIR="build/python/lib/python$PY_VERSION/site-packages"
PIP="pip$PY_VERSION"
mkdir -p $PY_DIR
PIP install -q -q -q $PACKAGE -t $PY_DIR
cd build
zip -q -r "../$PACKAGE/layer-$PY_VERSION.zip" .
cd ..
rm -r build
LAMBDA_LAYER_NAME="$PACKAGE-$PY_VERSION"
LAMBDA_LAYER_NAME="${LAMBDA_LAYER_NAME//./_}"
aws lambda publish-layer-version --layer-name "$LAMBDA_LAYER_NAME" --zip-file fileb://./$PACKAGE/layer-$PY_VERSION.zip --compatible-runtimes "python$PY_VERSION" --output json | jq
done
rm -r "./$PACKAGE"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment