From b6b8c780f0f35c15715877dbdd1de590f4bc0e57 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 16:16:43 +0200 Subject: [PATCH 01/45] Add possibility to customize code path --- action.yml | 5 +++++ entrypoint.sh | 2 ++ 2 files changed, 7 insertions(+) diff --git a/action.yml b/action.yml index f06de98..58e08bb 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: lambda_function_name: description: The Lambda function name. Check the AWS docs/readme for examples. required: true + code_path: + description: Path to the code. + required: false + default: './' runs: using: 'docker' image: 'Dockerfile' @@ -19,6 +23,7 @@ runs: - ${{ inputs.requirements_txt }} - ${{ inputs.lambda_layer_arn }} - ${{ inputs.lambda_function_name }} + - ${{ inputs.code_path }} branding: icon: 'layers' color: 'yellow' diff --git a/entrypoint.sh b/entrypoint.sh index 0ffdfed..a184938 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,8 @@ #!/bin/bash set -e +cd "${INPUT_CODE_PATH}" + install_zip_dependencies(){ echo "Installing and zipping dependencies..." mkdir python From 5d53a672376fa19259ddd72af6d37ceffab46541 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 16:41:29 +0200 Subject: [PATCH 02/45] Add possibility to publish layer and/or lambda --- entrypoint.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index a184938..6fe470a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,6 +3,8 @@ set -e cd "${INPUT_CODE_PATH}" +env + install_zip_dependencies(){ echo "Installing and zipping dependencies..." mkdir python @@ -30,10 +32,9 @@ update_function_layers(){ } deploy_lambda_function(){ - install_zip_dependencies - publish_dependencies_as_layer - publish_function_code - update_function_layers + [ -z "$INPUT_LAMBDA_LAYER_ARN" ] && install_zip_dependencies && publish_dependencies_as_layer + [ -z "$INPUT_LAMBDA_FUNCTION_NAME" ] && publish_function_code + [ -z "$INPUT_LAMBDA_LAYER_ARN" ] && [ -z "$INPUT_LAMBDA_FUNCTION_NAME" ] && update_function_layers } deploy_lambda_function From 9729c7b16632eb085e4a00cf1e78ffe0a1c038b9 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 16:52:41 +0200 Subject: [PATCH 03/45] Test --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 6fe470a..9661112 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,6 @@ #!/bin/bash set -e +set -x cd "${INPUT_CODE_PATH}" From 157fac3e7c1b7601a42cc539707b68ded0557cb9 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 16:55:31 +0200 Subject: [PATCH 04/45] Test --- entrypoint.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9661112..9c636c0 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -33,9 +33,9 @@ update_function_layers(){ } deploy_lambda_function(){ - [ -z "$INPUT_LAMBDA_LAYER_ARN" ] && install_zip_dependencies && publish_dependencies_as_layer - [ -z "$INPUT_LAMBDA_FUNCTION_NAME" ] && publish_function_code - [ -z "$INPUT_LAMBDA_LAYER_ARN" ] && [ -z "$INPUT_LAMBDA_FUNCTION_NAME" ] && update_function_layers + [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && install_zip_dependencies && publish_dependencies_as_layer + [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && publish_function_code + [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && update_function_layers } deploy_lambda_function From 9573f36c2eb961b49ba11179269623418dc4b375 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 17:39:55 +0200 Subject: [PATCH 05/45] Test --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 9c636c0..d502615 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -36,6 +36,7 @@ deploy_lambda_function(){ [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && install_zip_dependencies && publish_dependencies_as_layer [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && publish_function_code [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && update_function_layers + true } deploy_lambda_function From a43611f6a88ff12e3a53e3fd09efd69dc0dcf3ea Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Wed, 27 Oct 2021 18:24:07 +0200 Subject: [PATCH 06/45] Remove debug cmd --- entrypoint.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index d502615..f11f46e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,11 +1,8 @@ #!/bin/bash set -e -set -x cd "${INPUT_CODE_PATH}" -env - install_zip_dependencies(){ echo "Installing and zipping dependencies..." mkdir python From ccfd35888a9d0abdcfbf1a9ced3df8f4383f9608 Mon Sep 17 00:00:00 2001 From: Cyril Feraudet Date: Mon, 22 Nov 2021 14:26:58 +0100 Subject: [PATCH 07/45] Use s3 bucket to store lambda code --- .gitignore | 1 + action.yml | 4 ++++ entrypoint.sh | 1 + 3 files changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ee84da --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.sw* diff --git a/action.yml b/action.yml index 58e08bb..b536bfe 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,9 @@ inputs: lambda_function_name: description: The Lambda function name. Check the AWS docs/readme for examples. required: true + s3_bucket: + description: S3 bucket where upload lambda zip code. + required: true code_path: description: Path to the code. required: false @@ -24,6 +27,7 @@ runs: - ${{ inputs.lambda_layer_arn }} - ${{ inputs.lambda_function_name }} - ${{ inputs.code_path }} + - ${{ inputs.s3_bucket }} branding: icon: 'layers' color: 'yellow' diff --git a/entrypoint.sh b/entrypoint.sh index f11f46e..f63382e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -21,6 +21,7 @@ publish_dependencies_as_layer(){ publish_function_code(){ echo "Deploying the code itself..." zip -r code.zip . -x \*.git\* + aws s3 cp code.zip s3://${INPUT_S3_BUCKET}/${INPUT_LAMBDA_FUNCTION_NAME}.zip aws lambda update-function-code --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --zip-file fileb://code.zip } From dc560174289b5387b57a118b43cc1e75fc0dbf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Mon, 22 Nov 2021 16:34:25 +0100 Subject: [PATCH 08/45] add architectures and runtimes parameters --- action.yml | 9 +++++++++ entrypoint.sh | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index b536bfe..7a74881 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,13 @@ inputs: description: Path to the code. required: false default: './' + architectures: + description: Target architectures + required: false + default: 'x86_64' + runtimes: + description: Compatible runtimes in space-separated string + required: true runs: using: 'docker' image: 'Dockerfile' @@ -28,6 +35,8 @@ runs: - ${{ inputs.lambda_function_name }} - ${{ inputs.code_path }} - ${{ inputs.s3_bucket }} + - ${{ inputs.architectures }} + - ${{ inputs.runtimes }} branding: icon: 'layers' color: 'yellow' diff --git a/entrypoint.sh b/entrypoint.sh index f63382e..accf75c 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -12,7 +12,7 @@ install_zip_dependencies(){ publish_dependencies_as_layer(){ echo "Publishing dependencies as a layer..." - local result=$(aws lambda publish-layer-version --layer-name "${INPUT_LAMBDA_LAYER_ARN}" --zip-file fileb://dependencies.zip) + local result=$(aws lambda publish-layer-version --layer-name "${INPUT_LAMBDA_LAYER_ARN}" --compatible-runtimes ${INPUT_RUNTIMES} --compatible-architectures ${INPUT_ARCHITECTURES} --zip-file fileb://dependencies.zip) LAYER_VERSION=$(jq '.Version' <<< "$result") rm -rf python rm dependencies.zip @@ -22,12 +22,12 @@ publish_function_code(){ echo "Deploying the code itself..." zip -r code.zip . -x \*.git\* aws s3 cp code.zip s3://${INPUT_S3_BUCKET}/${INPUT_LAMBDA_FUNCTION_NAME}.zip - aws lambda update-function-code --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --zip-file fileb://code.zip + aws lambda update-function-code --architectures ${INPUT_ARCHITECTURES} --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --zip-file fileb://code.zip } update_function_layers(){ echo "Using the layer in the function..." - aws lambda update-function-configuration --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --layers "${INPUT_LAMBDA_LAYER_ARN}:${LAYER_VERSION}" + aws lambda update-function-configuration --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --runtime "${INPUT_RUNTIMES%% *}" --layers "${INPUT_LAMBDA_LAYER_ARN}:${LAYER_VERSION}" } deploy_lambda_function(){ From 593b3139bbcbf446a9461eb9d78bb15e1b7da310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 18:14:12 +0100 Subject: [PATCH 09/45] Fully backward uncompatible upgrade. Now deploy.json contains a list of blocks per AWS Lambda resource --- action.yml | 60 ++++++++++++++------- entrypoint.sh | 146 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 159 insertions(+), 47 deletions(-) diff --git a/action.yml b/action.yml index 7a74881..696100f 100644 --- a/action.yml +++ b/action.yml @@ -1,42 +1,62 @@ name: Py Lambda Deploy author: Mariam Maarouf -description: Deploy python code to AWS Lambda with dependencies in a separate layer. +description: | + Deploy python code to AWS Lambda with dependencies in a separate layer. inputs: - requirements_txt: - description: the name/path to the requirements.txt file + target: + description: lambda or layer required: true - default: 'requirements.txt' - lambda_layer_arn: - description: The ARN for the Lambda layer the dependencies should be pushed to without the version (every push is a new version). - required: true - lambda_function_name: - description: The Lambda function name. Check the AWS docs/readme for examples. - required: true - s3_bucket: - description: S3 bucket where upload lambda zip code. + name: + description: target name required: true - code_path: - description: Path to the code. - required: false - default: './' architectures: description: Target architectures required: false default: 'x86_64' runtimes: description: Compatible runtimes in space-separated string + required: false + default: 'python3.9 python3.8 python3.7 python3.6' + path: + description: Path to the code. + required: false + pip: + description: The name/path to the requirements.pip file + required: false + patterns: + description: Regex patterns to gather files for the archive (not used yet) + required: false + excludes: + description: zip -x patterns to exclude files from the archive + required: false + lambda_layer_arn: + description: | + The ARN for the Lambda layer the dependencies should be pushed to + without the version (every push is a new version). + required: false + lambda_function_name: + description: | + The Lambda function name. Check the AWS docs/readme for examples. + required: false + s3_bucket: + description: S3 bucket where upload lambda zip code. required: true runs: using: 'docker' image: 'Dockerfile' args: - - ${{ inputs.requirements_txt }} + - ${{ inputs.target }} + - ${{ inputs.name }} + - ${{ inputs.architectures }} + - ${{ inputs.runtimes }} + - ${{ inputs.path }} + - ${{ inputs.pip }} + - ${{ inputs.patterns }} + - ${{ inputs.excludes }} + - ${{ inputs.layers }} - ${{ inputs.lambda_layer_arn }} - ${{ inputs.lambda_function_name }} - - ${{ inputs.code_path }} - ${{ inputs.s3_bucket }} - - ${{ inputs.architectures }} - - ${{ inputs.runtimes }} branding: icon: 'layers' color: 'yellow' diff --git a/entrypoint.sh b/entrypoint.sh index accf75c..e7c331a 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,41 +1,133 @@ #!/bin/bash set -e -cd "${INPUT_CODE_PATH}" -install_zip_dependencies(){ - echo "Installing and zipping dependencies..." - mkdir python - pip install --target=python -r "${INPUT_REQUIREMENTS_TXT}" - zip -r dependencies.zip ./python + + +name="$(basename $0)" + + +die() +{ + echo ERROR: $@ >&2 + exit -1 } -publish_dependencies_as_layer(){ - echo "Publishing dependencies as a layer..." - local result=$(aws lambda publish-layer-version --layer-name "${INPUT_LAMBDA_LAYER_ARN}" --compatible-runtimes ${INPUT_RUNTIMES} --compatible-architectures ${INPUT_ARCHITECTURES} --zip-file fileb://dependencies.zip) - LAYER_VERSION=$(jq '.Version' <<< "$result") - rm -rf python - rm dependencies.zip +log() +{ + echo $name: $@ } -publish_function_code(){ - echo "Deploying the code itself..." - zip -r code.zip . -x \*.git\* - aws s3 cp code.zip s3://${INPUT_S3_BUCKET}/${INPUT_LAMBDA_FUNCTION_NAME}.zip - aws lambda update-function-code --architectures ${INPUT_ARCHITECTURES} --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --zip-file fileb://code.zip + + + +get_last_layer_version_arn() +{ + layer_name="$1" + architecture="$2" + runtime="$3" + # TODO: could try all combinations of arch and runtimes + result=$(aws lambda list-layer-versions --layer-name "$layer_name" \ + --compatible-architecture "$architecture" \ + --compatible-runtime "$runtime" \ + --max-items 1) + jq .LayerVersions[0].LayerVersionArn <<< "$result" } -update_function_layers(){ - echo "Using the layer in the function..." - aws lambda update-function-configuration --function-name "${INPUT_LAMBDA_FUNCTION_NAME}" --runtime "${INPUT_RUNTIMES%% *}" --layers "${INPUT_LAMBDA_LAYER_ARN}:${LAYER_VERSION}" + +make_archive() +{ + log "Building $INPUT_NAME $INPUT_TARGET archive..." + archive="$(realpath .)/archive.zip" + trap "rm -f -- '$archive'" EXIT + log "Installing codes..." + if [ -n "$INPUT_PATH" ]; then + for path in $INPUT_PATH; do + pushd $path + zip -r $archive . -x $INPUT_PATTERNS #\*.git\* \*/__pycache__\* + popd + done + fi + log "Installing dependencies..." + if [ -n "$INPUT_PIP" ]; then + tempdir=$(mktemp -d pip.XXXXXXXXXX) + trap "rm -f -- '$archive' '$tempdir'" EXIT + for path in $INPUT_PIP; do + pip install -t "$tempdir" -r "$path" + done + pushd "$tempdir" + zip -r $archive . -x $INPUT_PATTERNS + popd + rm -f -- "$tempdir" + trap "rm -f -- '$archive'" EXIT + fi + echo -n "$archive" } -deploy_lambda_function(){ - [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && install_zip_dependencies && publish_dependencies_as_layer - [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && publish_function_code - [ -n "$INPUT_LAMBDA_LAYER_ARN" ] && [ -n "$INPUT_LAMBDA_FUNCTION_NAME" ] && update_function_layers - true +list_layer_version_arns() +{ + arns= + for layer_name in $@; do + layer_json="$(get_last_layer_version_arn "$layer_name")" + arn="$(jq .LayerVersions[0].LayerVersionArn <<< $layer_json)" + arns="$arns $arn" + done + echo -n $arns } -deploy_lambda_function -echo "Done." +deploy_lambda_function() +{ + log "Deploying lambda function: $INPUT_NAME..." + s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" + aws s3 cp "$archive" "$s3_url" + aws lambda update-function-code \ + --architectures "$INPUT_ARCHITECTURES" \ + --function-name "$INPUT_NAME" \ + --zip-file "fileb://$archive" + opts="" + if [ -n "$INPUT_LAYERS" ]; then + layers=$(list_layer_version_arns "$INPUT_LAYERS") + opts="--layers $layers" + fi + aws lambda update-function-configuration --function-name "${INPUT_NAME}" \ + --runtime "${INPUT_RUNTIMES%% *}" $opts + rm -f -- "$archive" + trap - EXIT +} + +deploy_lambda_layer() +{ + log "Deploying lambda layer: ${INPUT_NAME}..." + local s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" + aws s3 cp "$archive" "$s3_url" + local result="$(aws lambda publish-layer-version \ + --layer-name "$INPUT_LAYER_LAMBDA_ARN" \ + --compatible-architectures "$INPUT_ARCHITECTURES" \ + --compatible-runtimes "$INPUT_RUNTIMES" \ + --zip-file "fileb://$archive" \ + )" + arn="$(jq .LayerVersionArn <<< "$result")" + echo -n $arn +} + + +TAG="${INPUT_NAME#*#}" +INPUT_NAME="${INPUT_NAME#*}" + + +case "$INPUT_TARGET" in + lambda) + make_archive + deploy_lambda_function + ;; + layer) + make_archive + deploy_lambda_layer + ;; + *) + die Invalid resource target: $INPUT_TARGET + ;; +esac + + +log "Done." From 2f64dd6d7e6bf06010a4fc62c499e519417417b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 18:55:30 +0100 Subject: [PATCH 10/45] upgrade python version from 3.6 to 3.9 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 869eedd..feb009d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.6 +FROM python:3.9 RUN apt-get update RUN apt-get install -y jq zip From ebf372eca69eeffd9f4e1a16e654ec1c70745edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:12:47 +0100 Subject: [PATCH 11/45] fix zip -x arguments management --- entrypoint.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index e7c331a..cb03d8e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -43,8 +43,9 @@ make_archive() log "Installing codes..." if [ -n "$INPUT_PATH" ]; then for path in $INPUT_PATH; do + [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= pushd $path - zip -r $archive . -x $INPUT_PATTERNS #\*.git\* \*/__pycache__\* + zip -r $archive . $opts popd done fi @@ -56,7 +57,8 @@ make_archive() pip install -t "$tempdir" -r "$path" done pushd "$tempdir" - zip -r $archive . -x $INPUT_PATTERNS + [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= + zip -r $archive . $opts popd rm -f -- "$tempdir" trap "rm -f -- '$archive'" EXIT From 516bc232e5f693ab5b0876e8ec1824c178225dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:26:56 +0100 Subject: [PATCH 12/45] fix rm arguments in entrypoint.sh --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index cb03d8e..ecb7e91 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -52,7 +52,7 @@ make_archive() log "Installing dependencies..." if [ -n "$INPUT_PIP" ]; then tempdir=$(mktemp -d pip.XXXXXXXXXX) - trap "rm -f -- '$archive' '$tempdir'" EXIT + trap "rm -rf -- '$archive' '$tempdir'" EXIT for path in $INPUT_PIP; do pip install -t "$tempdir" -r "$path" done @@ -60,7 +60,7 @@ make_archive() [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= zip -r $archive . $opts popd - rm -f -- "$tempdir" + rm -rf -- "$tempdir" trap "rm -f -- '$archive'" EXIT fi echo -n "$archive" From cbaa6b353f10c27c7db810ee1aabcfbbf86d254e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:28:07 +0100 Subject: [PATCH 13/45] add missing layers input to action.yml --- action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/action.yml b/action.yml index 696100f..4e5f54a 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: excludes: description: zip -x patterns to exclude files from the archive required: false + layers: + description: Lambda Layers to add to Lambda Function + required: false lambda_layer_arn: description: | The ARN for the Lambda layer the dependencies should be pushed to From f8114c67e0a05400b9ae5609b5a53de0a4a21866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:33:40 +0100 Subject: [PATCH 14/45] fix INPUT_NAME#TAG splitting pattern --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index ecb7e91..032d880 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -114,7 +114,7 @@ deploy_lambda_layer() TAG="${INPUT_NAME#*#}" -INPUT_NAME="${INPUT_NAME#*}" +INPUT_NAME="${INPUT_NAME%#*}" case "$INPUT_TARGET" in From bc30139364b8969bbed51905ca4f48a5fa2aacc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:42:42 +0100 Subject: [PATCH 15/45] fix layer version retrieval --- entrypoint.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 032d880..d435ac5 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -24,12 +24,10 @@ log() get_last_layer_version_arn() { layer_name="$1" - architecture="$2" - runtime="$3" # TODO: could try all combinations of arch and runtimes result=$(aws lambda list-layer-versions --layer-name "$layer_name" \ - --compatible-architecture "$architecture" \ - --compatible-runtime "$runtime" \ + --compatible-architecture "${INPUT_ARCHITECTURES% *}" \ + --compatible-runtime "${INPUT_RUNTIME% *}" \ --max-items 1) jq .LayerVersions[0].LayerVersionArn <<< "$result" } From 574bf895028e21460be97f184f7ce6ab84ab6e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 19:49:02 +0100 Subject: [PATCH 16/45] fix again --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index d435ac5..4888581 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -26,8 +26,8 @@ get_last_layer_version_arn() layer_name="$1" # TODO: could try all combinations of arch and runtimes result=$(aws lambda list-layer-versions --layer-name "$layer_name" \ - --compatible-architecture "${INPUT_ARCHITECTURES% *}" \ - --compatible-runtime "${INPUT_RUNTIME% *}" \ + --compatible-architecture "${INPUT_ARCHITECTURES%% *}" \ + --compatible-runtime "${INPUT_RUNTIMES%% *}" \ --max-items 1) jq .LayerVersions[0].LayerVersionArn <<< "$result" } From a46ee9ae37acb414854a4b42bcffdae7802da311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 21:57:56 +0100 Subject: [PATCH 17/45] fix last layer version retrival --- entrypoint.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 4888581..ba17bf9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -29,7 +29,8 @@ get_last_layer_version_arn() --compatible-architecture "${INPUT_ARCHITECTURES%% *}" \ --compatible-runtime "${INPUT_RUNTIMES%% *}" \ --max-items 1) - jq .LayerVersions[0].LayerVersionArn <<< "$result" + arn="$(jq -e .LayerVersions[0].LayerVersionArn <<< "$result")" + [ $? == 0 ] && echo -n $arn || return -1 } @@ -68,9 +69,8 @@ list_layer_version_arns() { arns= for layer_name in $@; do - layer_json="$(get_last_layer_version_arn "$layer_name")" - arn="$(jq .LayerVersions[0].LayerVersionArn <<< $layer_json)" - arns="$arns $arn" + layer_arn="$(get_last_layer_version_arn "$layer_name")" + arns="$arns $layer_arn" done echo -n $arns } @@ -84,7 +84,7 @@ deploy_lambda_function() --architectures "$INPUT_ARCHITECTURES" \ --function-name "$INPUT_NAME" \ --zip-file "fileb://$archive" - opts="" + opts= if [ -n "$INPUT_LAYERS" ]; then layers=$(list_layer_version_arns "$INPUT_LAYERS") opts="--layers $layers" From ee63d57e7ab9bd974ca78ed0b0f6e5092b6aeb15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 22:17:06 +0100 Subject: [PATCH 18/45] fix get_last_layer_version_arn again --- entrypoint.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index ba17bf9..7a7727e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -29,8 +29,9 @@ get_last_layer_version_arn() --compatible-architecture "${INPUT_ARCHITECTURES%% *}" \ --compatible-runtime "${INPUT_RUNTIMES%% *}" \ --max-items 1) - arn="$(jq -e .LayerVersions[0].LayerVersionArn <<< "$result")" - [ $? == 0 ] && echo -n $arn || return -1 + arn="$(jq -e .LayerVersions[0].LayerVersionArn <<< "$result"|cut -d\" -f2)" + [ $? != 0 ] || [ "$arn" == "null" ] && return 1 + echo -n $arn } From ea5f852f41294c3d8266c5739147d5c9d2ceb473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 22:38:30 +0100 Subject: [PATCH 19/45] fix mistyped variable name in entrypoint.sh --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 7a7727e..f616292 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -102,7 +102,7 @@ deploy_lambda_layer() local s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" local result="$(aws lambda publish-layer-version \ - --layer-name "$INPUT_LAYER_LAMBDA_ARN" \ + --layer-name "$INPUT_LAMBDA_LAYER_ARN" \ --compatible-architectures "$INPUT_ARCHITECTURES" \ --compatible-runtimes "$INPUT_RUNTIMES" \ --zip-file "fileb://$archive" \ From 5d3445a5e37913d56ed2ca756c23db614c60547d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 22:41:45 +0100 Subject: [PATCH 20/45] prevent deploy_lambda_layer() from failing silently --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index f616292..e14bf78 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -108,6 +108,7 @@ deploy_lambda_layer() --zip-file "fileb://$archive" \ )" arn="$(jq .LayerVersionArn <<< "$result")" + [ $? != 0 ] || [ "$arn" == "null" ] && return 1 echo -n $arn } From 7e8a5ab48ef257d2c8fce046a2a32f3cc4adf129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 22:49:46 +0100 Subject: [PATCH 21/45] fix entrypoint.sh again --- entrypoint.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index e14bf78..d371e72 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -101,12 +101,12 @@ deploy_lambda_layer() log "Deploying lambda layer: ${INPUT_NAME}..." local s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" - local result="$(aws lambda publish-layer-version \ - --layer-name "$INPUT_LAMBDA_LAYER_ARN" \ - --compatible-architectures "$INPUT_ARCHITECTURES" \ - --compatible-runtimes "$INPUT_RUNTIMES" \ - --zip-file "fileb://$archive" \ - )" + local result="$(aws lambda publish-layer-version \ + --layer-name "$INPUT_LAMBDA_LAYER_ARN" \ + --compatible-architectures $INPUT_ARCHITECTURES \ + --compatible-runtimes $INPUT_RUNTIMES \ + --zip-file "fileb://$archive" \ + )" arn="$(jq .LayerVersionArn <<< "$result")" [ $? != 0 ] || [ "$arn" == "null" ] && return 1 echo -n $arn From e3254a202a826cfd401b5746d94ea1fd34b9c92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 23:07:43 +0100 Subject: [PATCH 22/45] fix zip -x parameters --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index d371e72..cc5eade 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -42,8 +42,8 @@ make_archive() trap "rm -f -- '$archive'" EXIT log "Installing codes..." if [ -n "$INPUT_PATH" ]; then + [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= for path in $INPUT_PATH; do - [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= pushd $path zip -r $archive . $opts popd From 19de0573f6b793979c8cf07179b4b7c616c62e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 23:21:26 +0100 Subject: [PATCH 23/45] add debug message on $INPUT_EXCLUDES in make_archive() --- entrypoint.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index cc5eade..7b411a8 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -5,20 +5,24 @@ set -e name="$(basename $0)" - +DEBUG=1 die() { - echo ERROR: $@ >&2 + echo ERROR: $name: $@ >&2 exit -1 } log() { - echo $name: $@ + echo INFO: $name: $@ } - +debug() +{ + grep -q yes\\\|1\\\|on\\\|true <<< $DEBUG || return 0 + echo DEBUG: $name: $@ +} get_last_layer_version_arn() @@ -40,12 +44,14 @@ make_archive() log "Building $INPUT_NAME $INPUT_TARGET archive..." archive="$(realpath .)/archive.zip" trap "rm -f -- '$archive'" EXIT + [ -n "$INPUT_EXCLUDES" ] && zip_opts="-x $INPUT_EXCLUDES" || zip_opts= + debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" + debug "zip_opts: $zip_opts" log "Installing codes..." if [ -n "$INPUT_PATH" ]; then - [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= for path in $INPUT_PATH; do pushd $path - zip -r $archive . $opts + zip -r $archive . $zip_opts popd done fi @@ -57,8 +63,7 @@ make_archive() pip install -t "$tempdir" -r "$path" done pushd "$tempdir" - [ -n "$INPUT_EXCLUDES" ] && opts="-x $INPUT_EXCLUDES" || opts= - zip -r $archive . $opts + zip -r $archive . $zip_opts popd rm -rf -- "$tempdir" trap "rm -f -- '$archive'" EXIT From 07e4c2c28fb403130685ca26d33290fc5d9729d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 23:30:06 +0100 Subject: [PATCH 24/45] more debug --- entrypoint.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 7b411a8..808d5dc 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,6 +51,7 @@ make_archive() if [ -n "$INPUT_PATH" ]; then for path in $INPUT_PATH; do pushd $path + debug "Running: zip -r $archive . $zip_opts" zip -r $archive . $zip_opts popd done @@ -63,6 +64,7 @@ make_archive() pip install -t "$tempdir" -r "$path" done pushd "$tempdir" + debug "Running: zip -r $archive . $zip_opts" zip -r $archive . $zip_opts popd rm -rf -- "$tempdir" From 0ebdda70f5236d36cb7a43116980a5e7cf077838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 23:50:35 +0100 Subject: [PATCH 25/45] fix zip -x opts processing --- entrypoint.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 808d5dc..642afdf 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -44,7 +44,11 @@ make_archive() log "Building $INPUT_NAME $INPUT_TARGET archive..." archive="$(realpath .)/archive.zip" trap "rm -f -- '$archive'" EXIT - [ -n "$INPUT_EXCLUDES" ] && zip_opts="-x $INPUT_EXCLUDES" || zip_opts= + if [ -z "$INPUT_EXCLUDES" ]; then + zip_opts= + else + zip_opts="-x ${INPUT_EXCLUDES/\*/\\*}" + fi debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" debug "zip_opts: $zip_opts" log "Installing codes..." From 95311f680c97ce6bc1928523a543d0e00c4a1b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 23 Nov 2021 23:59:29 +0100 Subject: [PATCH 26/45] try again --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 642afdf..95c9ab2 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -47,7 +47,7 @@ make_archive() if [ -z "$INPUT_EXCLUDES" ]; then zip_opts= else - zip_opts="-x ${INPUT_EXCLUDES/\*/\\*}" + zip_opts="-x $(for w in $INPUT_EXCLUDES; do echo ${w/\*/\\\\*}; done |xargs)" fi debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" debug "zip_opts: $zip_opts" From 27173b29013165709c578559a5ea2c41c876957d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 09:44:13 +0100 Subject: [PATCH 27/45] try again --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 95c9ab2..d21aea9 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -47,7 +47,7 @@ make_archive() if [ -z "$INPUT_EXCLUDES" ]; then zip_opts= else - zip_opts="-x $(for w in $INPUT_EXCLUDES; do echo ${w/\*/\\\\*}; done |xargs)" + zip_opts="-x $(sed -e s#\*#\\\\*#g <<< $INPUT_EXCLUDES)" fi debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" debug "zip_opts: $zip_opts" From 42ed5edb36e2b425c5ecfe9a8c3a22e0134f105c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 09:56:15 +0100 Subject: [PATCH 28/45] try again --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index d21aea9..9ebad3b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -55,7 +55,7 @@ make_archive() if [ -n "$INPUT_PATH" ]; then for path in $INPUT_PATH; do pushd $path - debug "Running: zip -r $archive . $zip_opts" + debug Running: zip -r $archive . $zip_opts zip -r $archive . $zip_opts popd done From cc0e0466b1be196e3bed92aa6cb88a993b617057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 10:29:36 +0100 Subject: [PATCH 29/45] add set -x to entrypoint.sh --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9ebad3b..9b26f8d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,6 @@ #!/bin/bash set -e - +set -x @@ -55,7 +55,7 @@ make_archive() if [ -n "$INPUT_PATH" ]; then for path in $INPUT_PATH; do pushd $path - debug Running: zip -r $archive . $zip_opts + debug "Running: zip -r $archive . $zip_opts" zip -r $archive . $zip_opts popd done From 8a8088697893ac01a5e85233f248415fa2476a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 11:42:58 +0100 Subject: [PATCH 30/45] fix zip -x parameters using set -f --- entrypoint.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9b26f8d..ae70f28 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -44,10 +44,11 @@ make_archive() log "Building $INPUT_NAME $INPUT_TARGET archive..." archive="$(realpath .)/archive.zip" trap "rm -f -- '$archive'" EXIT + set -f if [ -z "$INPUT_EXCLUDES" ]; then zip_opts= else - zip_opts="-x $(sed -e s#\*#\\\\*#g <<< $INPUT_EXCLUDES)" + zip_opts="-x $INPUT_EXCLUDES" fi debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" debug "zip_opts: $zip_opts" @@ -74,6 +75,7 @@ make_archive() rm -rf -- "$tempdir" trap "rm -f -- '$archive'" EXIT fi + set +f echo -n "$archive" } From 62d0db473d48d5c6cd475241d4cc5c4377353246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 13:23:31 +0100 Subject: [PATCH 31/45] make archive for layers to have all the contents in python/ directory --- entrypoint.sh | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index ae70f28..3650317 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -43,40 +43,45 @@ make_archive() { log "Building $INPUT_NAME $INPUT_TARGET archive..." archive="$(realpath .)/archive.zip" - trap "rm -f -- '$archive'" EXIT set -f - if [ -z "$INPUT_EXCLUDES" ]; then + trap "rm -f -- '$archive'" EXIT + if [ -z "$INPUT_EXCLUDES" ]; then zip_opts= else zip_opts="-x $INPUT_EXCLUDES" fi debug "INPUT_EXCLUDES: $INPUT_EXCLUDES" debug "zip_opts: $zip_opts" - log "Installing codes..." + set +f + tempdir=$(mktemp -d pip.XXXXXXXXXX) + trap "rm -rf -- '$archive' '$tempdir'" EXIT + mkdir "$tempdir/python" if [ -n "$INPUT_PATH" ]; then + log "Installing codes... : $INPUT_PATH" for path in $INPUT_PATH; do - pushd $path - debug "Running: zip -r $archive . $zip_opts" - zip -r $archive . $zip_opts - popd + ln -s "$path/*" "$tempdir/python/" done fi - log "Installing dependencies..." if [ -n "$INPUT_PIP" ]; then - tempdir=$(mktemp -d pip.XXXXXXXXXX) - trap "rm -rf -- '$archive' '$tempdir'" EXIT + log "Installing dependencies... : $INPUT_PIP" for path in $INPUT_PIP; do - pip install -t "$tempdir" -r "$path" + pip install -t "$tempdir/python/" -r "$path" done + fi + log "Zipping archive..." + set -f + if [ "$INPUT_TARGET" == "layer" ]; then pushd "$tempdir" - debug "Running: zip -r $archive . $zip_opts" - zip -r $archive . $zip_opts + zip -r $archive python $zip_args + popd + else + pushd "$tempdir/python" + zip -r $archive . $zip_args popd - rm -rf -- "$tempdir" - trap "rm -f -- '$archive'" EXIT fi set +f - echo -n "$archive" + rm -rf -- "$tempdir" + trap "rm -f -- '$archive'" EXIT } list_layer_version_arns() From 8a01b8f01fce59fc7269cbc465567d306cba2b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 13:43:53 +0100 Subject: [PATCH 32/45] fix misnamed variable --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 3650317..6585a78 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -72,11 +72,11 @@ make_archive() set -f if [ "$INPUT_TARGET" == "layer" ]; then pushd "$tempdir" - zip -r $archive python $zip_args + zip -r $archive python $zip_opts popd else pushd "$tempdir/python" - zip -r $archive . $zip_args + zip -r $archive . $zip_opts popd fi set +f From de341e1fce04c4765d9f49fde9e6ac8c05de3881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 13:51:48 +0100 Subject: [PATCH 33/45] fix ln arguments --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 6585a78..1627e4b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,7 +59,7 @@ make_archive() if [ -n "$INPUT_PATH" ]; then log "Installing codes... : $INPUT_PATH" for path in $INPUT_PATH; do - ln -s "$path/*" "$tempdir/python/" + ln -s "$(realpath $path)/*" "$tempdir/python/" done fi if [ -n "$INPUT_PIP" ]; then From dcab608c8c8b3e9b9918a7471c9b1c83467feda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 13:58:41 +0100 Subject: [PATCH 34/45] add -v opt to ln --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1627e4b..e083c4e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,7 +59,7 @@ make_archive() if [ -n "$INPUT_PATH" ]; then log "Installing codes... : $INPUT_PATH" for path in $INPUT_PATH; do - ln -s "$(realpath $path)/*" "$tempdir/python/" + ln -vs "$(realpath $path)/*" "$tempdir/python/" done fi if [ -n "$INPUT_PIP" ]; then From 44adf9120ef422eefde0bdfe8f9a0577ec3247ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 14:04:46 +0100 Subject: [PATCH 35/45] add ls -l --- entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index e083c4e..9a16230 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,7 +59,8 @@ make_archive() if [ -n "$INPUT_PATH" ]; then log "Installing codes... : $INPUT_PATH" for path in $INPUT_PATH; do - ln -vs "$(realpath $path)/*" "$tempdir/python/" + ls -l "$path" + ln -vs "$(realpath $path)/"* "$tempdir/python/" done fi if [ -n "$INPUT_PIP" ]; then From ba4994f38b3a6db869d3953c4ca095ced3c5ca69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 14:10:54 +0100 Subject: [PATCH 36/45] fix lambda function archive build --- entrypoint.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9a16230..449ac71 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,7 +59,6 @@ make_archive() if [ -n "$INPUT_PATH" ]; then log "Installing codes... : $INPUT_PATH" for path in $INPUT_PATH; do - ls -l "$path" ln -vs "$(realpath $path)/"* "$tempdir/python/" done fi From bf9403928052588bc5e0b1606161572f73c1982c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Wed, 24 Nov 2021 14:17:15 +0100 Subject: [PATCH 37/45] remove bash opt set -x --- entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 449ac71..c3bd33e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,12 +1,13 @@ #!/bin/bash set -e -set -x + name="$(basename $0)" DEBUG=1 + die() { echo ERROR: $name: $@ >&2 From 4605aa9f951a619bf91dce0140779671e19383f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 14:42:43 +0100 Subject: [PATCH 38/45] Disable update-function-code if the lambda function does not exist. Add retries on update-function-configuration. Add publish-function. --- entrypoint.sh | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index c3bd33e..a2fa354 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -100,17 +100,29 @@ deploy_lambda_function() log "Deploying lambda function: $INPUT_NAME..." s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" - aws lambda update-function-code \ - --architectures "$INPUT_ARCHITECTURES" \ - --function-name "$INPUT_NAME" \ - --zip-file "fileb://$archive" - opts= - if [ -n "$INPUT_LAYERS" ]; then - layers=$(list_layer_version_arns "$INPUT_LAYERS") - opts="--layers $layers" + if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null 2>&1 + then + aws lambda update-function-code \ + --architectures "$INPUT_ARCHITECTURES" \ + --function-name "$INPUT_NAME" \ + --zip-file "fileb://$archive" + opts= + if [ -n "$INPUT_LAYERS" ]; then + layers=$(list_layer_version_arns "$INPUT_LAYERS") + opts="--layers $layers" + fi + retry=4 + while ! aws lambda update-function-configuration \ + --function-name "${INPUT_NAME}" \ + --runtime "${INPUT_RUNTIMES%% *}" $opts; do + retry="$(($retry - 1))" + if [[ $retry -gt 0 ]]; then + die "Cannot update-function-configuration: ${INPUT_NAME}" + fi + sleep 1 + done + aws lambda publish-function "$INPUT_NAME" fi - aws lambda update-function-configuration --function-name "${INPUT_NAME}" \ - --runtime "${INPUT_RUNTIMES%% *}" $opts rm -f -- "$archive" trap - EXIT } From b5480599813e1e152ebf2570265a081abc9a9ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 14:55:46 +0100 Subject: [PATCH 39/45] Add log messages in deploy_lambda_function() --- entrypoint.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index a2fa354..19379c8 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -102,10 +102,11 @@ deploy_lambda_function() aws s3 cp "$archive" "$s3_url" if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null 2>&1 then + log "Updating lambda function code: ${INPUT_NAME}" aws lambda update-function-code \ --architectures "$INPUT_ARCHITECTURES" \ - --function-name "$INPUT_NAME" \ - --zip-file "fileb://$archive" + --function-name "$INPUT_NAME" \ + --zip-file "fileb://$archive" opts= if [ -n "$INPUT_LAYERS" ]; then layers=$(list_layer_version_arns "$INPUT_LAYERS") @@ -122,6 +123,8 @@ deploy_lambda_function() sleep 1 done aws lambda publish-function "$INPUT_NAME" + else + log "No lambda function found: $INPUT_NAME" fi rm -f -- "$archive" trap - EXIT From 2c1be5b2f677a9c76c41c3d8087fd5796f7ca99c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 15:08:47 +0100 Subject: [PATCH 40/45] update debug message --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 19379c8..a7ca3bb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -100,9 +100,9 @@ deploy_lambda_function() log "Deploying lambda function: $INPUT_NAME..." s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" + log "Updating lambda function code: ${INPUT_NAME}" if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null 2>&1 then - log "Updating lambda function code: ${INPUT_NAME}" aws lambda update-function-code \ --architectures "$INPUT_ARCHITECTURES" \ --function-name "$INPUT_NAME" \ From d5ded757e82eece2d764b999fba97546c06101a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 15:50:49 +0100 Subject: [PATCH 41/45] enable stderr on get-function operation --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index a7ca3bb..cf9a280 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -101,7 +101,7 @@ deploy_lambda_function() s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" log "Updating lambda function code: ${INPUT_NAME}" - if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null 2>&1 + if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null then aws lambda update-function-code \ --architectures "$INPUT_ARCHITECTURES" \ From 61cb04f6c7f60af3d51e6468ba38a4d67dd91ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 16:23:26 +0100 Subject: [PATCH 42/45] enable stdout on get-function operation --- entrypoint.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index cf9a280..96c3c01 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -101,8 +101,7 @@ deploy_lambda_function() s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" log "Updating lambda function code: ${INPUT_NAME}" - if aws lambda get-function --function-name "${INPUT_NAME}" >/dev/null - then + if aws lambda get-function --function-name "${INPUT_NAME}"; then aws lambda update-function-code \ --architectures "$INPUT_ARCHITECTURES" \ --function-name "$INPUT_NAME" \ From 5464894390aa7accde6a075ce8a0e1184fdff12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 17:11:14 +0100 Subject: [PATCH 43/45] add lambda_function_exists() tool --- entrypoint.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 96c3c01..4fb1c86 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -95,13 +95,19 @@ list_layer_version_arns() echo -n $arns } +lambda_function_exists() +{ + aws lambda list-functions | jq '.["Functions"][]["FunctionName"]' | \ + grep -q "$INPUT_NAME" +} + deploy_lambda_function() { log "Deploying lambda function: $INPUT_NAME..." s3_url="s3://${INPUT_S3_BUCKET}/${INPUT_NAME}.zip" aws s3 cp "$archive" "$s3_url" log "Updating lambda function code: ${INPUT_NAME}" - if aws lambda get-function --function-name "${INPUT_NAME}"; then + if lambda_function_exists; then aws lambda update-function-code \ --architectures "$INPUT_ARCHITECTURES" \ --function-name "$INPUT_NAME" \ From d210194a6d2165b0fc1f5c5f10a3b7b151a2668e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 18:26:02 +0100 Subject: [PATCH 44/45] fix publish-version --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 4fb1c86..64e8575 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -127,7 +127,7 @@ deploy_lambda_function() fi sleep 1 done - aws lambda publish-function "$INPUT_NAME" + aws lambda publish-version --function-name "$INPUT_NAME" else log "No lambda function found: $INPUT_NAME" fi From 9f357bc4521de8b9d5766a27c5210aa60346baa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Lee?= Date: Tue, 28 Dec 2021 18:49:35 +0100 Subject: [PATCH 45/45] add aws wait command to wait previous operation before sending the next one --- entrypoint.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 64e8575..6bab40e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -117,6 +117,8 @@ deploy_lambda_function() layers=$(list_layer_version_arns "$INPUT_LAYERS") opts="--layers $layers" fi + aws lambda wait function-updated --function-name "$INPUT_NAME" + log "Lambda function updated: $INPUT_NAME" retry=4 while ! aws lambda update-function-configuration \ --function-name "${INPUT_NAME}" \ @@ -127,7 +129,10 @@ deploy_lambda_function() fi sleep 1 done + aws lambda wait function-updated --function-name "$INPUT_NAME" + log "Lambda function configured: $INPUT_NAME" aws lambda publish-version --function-name "$INPUT_NAME" + log "Lambda function published: $INPUT_NAME" else log "No lambda function found: $INPUT_NAME" fi