Last active
August 10, 2022 22:01
-
-
Save kota65535/ebade565252b7913ff75ba6a4a79b9ff to your computer and use it in GitHub Desktop.
Create python lambda layer contents
This file contains 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 | |
set -eu -o pipefail | |
# echo to stderr | |
eecho() { echo "$@" 1>&2; } | |
usage() { | |
cat <<EOF | |
Usage: | |
bash $(basename "$0") <python-version> <requirements-file> | |
Description: | |
Create lambda layer zip file according to the requirements file. | |
Requirements: | |
docker, jq | |
Arguments: | |
python-version : Python version | |
requirements-file : Path of pip requirements file | |
EOF | |
} | |
# Check number of arguments | |
if [[ $# != 2 ]]; then | |
usage && exit 1 | |
fi | |
PYTHON_VERSION=$1 | |
REQUIREMENTS_FILE=$2 | |
if [[ ! -f ${REQUIREMENTS_FILE} ]]; then | |
eecho "[ERROR] requirements file '${REQUIREMENTS_FILE}' not found." | |
exit 1 | |
fi | |
DEST_DIR=$(mktemp -d) | |
cp "${REQUIREMENTS_FILE}" "${DEST_DIR}" | |
( | |
cd "${DEST_DIR}" | |
mkdir python | |
# Run pip install command inside the official python docker image | |
docker run --rm -u "${UID}:${UID}" -v "${DEST_DIR}:/work" -w /work "python:${PYTHON_VERSION}" pip install -r "${REQUIREMENTS_FILE##*/}" -t ./python >&2 | |
# Remove unneeded files | |
find python \( -name '__pycache__' -o -name '*.dist-info' \) -type d -print0 | xargs -0 rm -rf | |
rm -rf python/bin | |
# Return JSON for Terraform | |
jq -n --arg path "${DEST_DIR}" '{"path":$path}' | |
) |
This file contains 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
locals { | |
layer_zip_path = "${path.root}/.terraform/tmp/my-lambda-layer.zip" | |
} | |
data "external" "lambda_layer" { | |
program = ["./create_lambda_layer.sh", "3.9", "requirements.txt"] | |
} | |
data "archive_file" "lambda_layer" { | |
type = "zip" | |
output_path = local.layer_zip_path | |
source_dir = data.external.lambda_layer.result.path | |
output_file_mode = "0644" | |
} | |
resource "aws_lambda_layer_version" "main" { | |
layer_name = "my-lambda" | |
filename = data.archive_file.lambda_layer.output_path | |
compatible_runtimes = ["python3.9"] | |
source_code_hash = data.archive_file.lambda_layer.output_base64sha256 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment