Skip to content

Instantly share code, notes, and snippets.

@pseonghoon
Last active January 24, 2022 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pseonghoon/89a9421536016f52dff5d34a20e832dc to your computer and use it in GitHub Desktop.
Save pseonghoon/89a9421536016f52dff5d34a20e832dc to your computer and use it in GitHub Desktop.
This script creates a zip file for the AWS Lambda layer that includes AWSCLI.
#!/usr/bin/env bash
# Prerequisites:
# - pyenv
# - virutalenv
#
# Usage:
# Run the following command if you want to create a zip file for Python 3.7 Lambda runtime
# and you have installed Python 3.7.11 in your local computer with pyenv.
#
# $ sh zip-labmda-layer-awscli.sh 3.7.11
set -e
PYTHON_VER=$1
RE='([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)'
if [[ $PYTHON_VER =~ $RE ]]
then
MAJOR_VERSION=${BASH_REMATCH[1]}
MINOR_VERSION=${BASH_REMATCH[2]}
PYTHON_VER_SHORT="${MAJOR_VERSION}${MINOR_VERSION}"
else
echo "Usage: $0 <python-version-in-pyenv>"
echo "Example: $0 3.8.11"
exit 1
fi
CURRENT_DIR=$PWD
WORK_DIR=/tmp/lambda-layer-awscli-$PYTHON_VER
rm -rf $WORK_DIR
mkdir $WORK_DIR
LAYER_FILE_DIR=$WORK_DIR/layer-files
VE_DIR=$WORK_DIR/venv
mkdir $LAYER_FILE_DIR
mkdir $LAYER_FILE_DIR/bin
cd $WORK_DIR
pyenv local $PYTHON_VER
virtualenv -p `pyenv which python` $VE_DIR
source $VE_DIR/bin/activate
pip install awscli
deactivate
cp $VE_DIR/bin/aws $LAYER_FILE_DIR/bin/
cp -r $VE_DIR/lib/python$MAJOR_VERSION.$MINOR_VERSION/site-packages/* $LAYER_FILE_DIR/bin/
perl -i -pe 's|#!.*|#!/var/lang/bin/python| if ($. == 1)' $LAYER_FILE_DIR/bin/aws
cd $LAYER_FILE_DIR
ZIPFILE=$CURRENT_DIR/lambda-layer-awscli-python$PYTHON_VER_SHORT.zip
rm -f $ZIPFILE
zip -r $ZIPFILE *
echo "$ZIPFILE is created."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment