2022-06-27 12:00:32 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -xeo pipefail
|
|
|
|
|
2022-07-28 12:23:59 +00:00
|
|
|
WORKDIR=$(dirname "$0")
|
2022-11-23 14:00:15 +00:00
|
|
|
WORKDIR=$(readlink -f "${WORKDIR}")
|
2023-05-22 21:07:35 +00:00
|
|
|
DIR_NAME=$(basename "$WORKDIR")
|
2022-07-28 12:23:59 +00:00
|
|
|
cd "$WORKDIR"
|
|
|
|
|
2023-05-22 18:39:22 +00:00
|
|
|
# Do not deploy the lambda to AWS
|
|
|
|
DRY_RUN=${DRY_RUN:-}
|
|
|
|
# Python runtime to install dependencies
|
|
|
|
PY_VERSION=${PY_VERSION:-3.10}
|
2022-11-23 14:00:15 +00:00
|
|
|
PY_EXEC="python${PY_VERSION}"
|
2023-05-22 18:39:22 +00:00
|
|
|
# Image to build the lambda zip package
|
2023-05-31 21:56:39 +00:00
|
|
|
DOCKER_IMAGE="public.ecr.aws/lambda/python:${PY_VERSION}"
|
2023-05-22 18:39:22 +00:00
|
|
|
# Rename the_lambda_name directory to the-lambda-name lambda in AWS
|
2023-05-22 21:07:35 +00:00
|
|
|
LAMBDA_NAME=${DIR_NAME//_/-}
|
2023-05-22 18:39:22 +00:00
|
|
|
# The name of directory with lambda code
|
2022-07-28 12:23:59 +00:00
|
|
|
PACKAGE=lambda-package
|
2024-02-12 16:56:05 +00:00
|
|
|
|
|
|
|
# Do not rebuild and deploy the archive if it's newer than sources
|
|
|
|
if [ -e "$PACKAGE.zip" ] && [ -z "$FORCE" ]; then
|
|
|
|
REBUILD=""
|
|
|
|
for src in app.py build_and_deploy_archive.sh requirements.txt lambda_shared/*; do
|
|
|
|
if [ "$src" -nt "$PACKAGE.zip" ]; then
|
|
|
|
REBUILD=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ -n "$REBUILD" ] || exit 0
|
|
|
|
fi
|
|
|
|
|
2024-02-29 21:00:29 +00:00
|
|
|
docker_cmd=(
|
|
|
|
docker run -i --net=host --rm --user="${UID}" -e HOME=/tmp --entrypoint=/bin/bash
|
|
|
|
--volume="${WORKDIR}/..:/ci" --workdir="/ci/${DIR_NAME}" "${DOCKER_IMAGE}"
|
|
|
|
)
|
2022-07-28 12:23:59 +00:00
|
|
|
rm -rf "$PACKAGE" "$PACKAGE".zip
|
2022-08-10 12:37:06 +00:00
|
|
|
mkdir "$PACKAGE"
|
2022-07-28 12:23:59 +00:00
|
|
|
cp app.py "$PACKAGE"
|
2022-08-10 12:37:06 +00:00
|
|
|
if [ -f requirements.txt ]; then
|
|
|
|
VENV=lambda-venv
|
2024-02-09 15:54:00 +00:00
|
|
|
rm -rf "$VENV"
|
2024-02-29 21:00:29 +00:00
|
|
|
"${docker_cmd[@]}" -ex <<EOF
|
|
|
|
'$PY_EXEC' -m venv '$VENV' &&
|
|
|
|
source '$VENV/bin/activate' &&
|
|
|
|
pip install -r requirements.txt &&
|
|
|
|
# To have consistent pyc files
|
|
|
|
find '$VENV/lib' -name '*.pyc' -delete
|
|
|
|
cp -rT '$VENV/lib/$PY_EXEC/site-packages/' '$PACKAGE'
|
|
|
|
rm -r '$PACKAGE'/{pip,pip-*,setuptools,setuptools-*}
|
2024-03-04 10:49:45 +00:00
|
|
|
chmod 0777 -R '$PACKAGE'
|
2024-02-29 21:00:29 +00:00
|
|
|
EOF
|
2022-08-10 12:37:06 +00:00
|
|
|
fi
|
2024-02-29 21:00:29 +00:00
|
|
|
# Create zip archive via python zipfile to have it cross-platform
|
|
|
|
"${docker_cmd[@]}" -ex <<EOF
|
|
|
|
cd '$PACKAGE'
|
|
|
|
find ! -type d -exec touch -t 201212121212 {} +
|
|
|
|
|
|
|
|
python <<'EOP'
|
|
|
|
import zipfile
|
|
|
|
import os
|
|
|
|
files_path = []
|
|
|
|
for root, _, files in os.walk('.'):
|
|
|
|
files_path.extend(os.path.join(root, file) for file in files)
|
|
|
|
# persistent file order
|
|
|
|
files_path.sort()
|
|
|
|
with zipfile.ZipFile('../$PACKAGE.zip', 'w') as zf:
|
|
|
|
for file in files_path:
|
|
|
|
zf.write(file)
|
|
|
|
EOP
|
|
|
|
EOF
|
2022-06-27 12:00:32 +00:00
|
|
|
|
2024-01-29 12:30:25 +00:00
|
|
|
ECHO=()
|
|
|
|
if [ -n "$DRY_RUN" ]; then
|
|
|
|
ECHO=(echo Run the following command to push the changes:)
|
2023-05-22 18:39:22 +00:00
|
|
|
fi
|
2024-01-29 12:30:25 +00:00
|
|
|
"${ECHO[@]}" aws lambda update-function-code --function-name "$LAMBDA_NAME" --zip-file fileb://"$WORKDIR/$PACKAGE".zip
|