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
|
|
|
|
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
|
|
|
|
rm -rf "$VENV" lambda-package.zip
|
2023-05-31 21:56:39 +00:00
|
|
|
docker run --rm --user="${UID}" -e HOME=/tmp --entrypoint=/bin/bash \
|
2023-05-22 21:07:35 +00:00
|
|
|
--volume="${WORKDIR}/..:/ci" --workdir="/ci/${DIR_NAME}" "${DOCKER_IMAGE}" \
|
2023-05-31 21:56:39 +00:00
|
|
|
-exc "
|
2022-11-23 14:00:15 +00:00
|
|
|
'$PY_EXEC' -m venv '$VENV' &&
|
|
|
|
source '$VENV/bin/activate' &&
|
|
|
|
pip install -r requirements.txt
|
|
|
|
"
|
2022-08-10 12:37:06 +00:00
|
|
|
cp -rT "$VENV/lib/$PY_EXEC/site-packages/" "$PACKAGE"
|
|
|
|
rm -r "$PACKAGE"/{pip,pip-*,setuptools,setuptools-*}
|
|
|
|
fi
|
|
|
|
( cd "$PACKAGE" && zip -9 -r ../"$PACKAGE".zip . )
|
2022-06-27 12:00:32 +00:00
|
|
|
|
2023-05-22 18:39:22 +00:00
|
|
|
if [ -z "$DRY_RUN" ]; then
|
2023-05-22 21:07:35 +00:00
|
|
|
aws lambda update-function-code --function-name "$LAMBDA_NAME" --zip-file fileb://"$WORKDIR/$PACKAGE".zip
|
2023-05-22 18:39:22 +00:00
|
|
|
fi
|