From 8e597cee7c8230b50e3ef08cf64958c8546c851d Mon Sep 17 00:00:00 2001 From: magnus Date: Tue, 17 Nov 2026 13:13:58 +0000 Subject: [PATCH] python-lambda-zip: set AWS region, add smoke-test-env The Lambda runtime always sets AWS_REGION, and handlers commonly create boto3 clients at import time, which fails without a region. Also allow passing other environment variables that code reads at import time. Co-Authored-By: Claude Sonnet 5.5 --- actions/python-lambda-zip/README.md | 1 + actions/python-lambda-zip/action.yml | 7 +++++++ actions/python-lambda-zip/build.sh | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/actions/python-lambda-zip/README.md b/actions/python-lambda-zip/README.md index b079c3b..e24a93a 100644 --- a/actions/python-lambda-zip/README.md +++ b/actions/python-lambda-zip/README.md @@ -50,6 +50,7 @@ The runner needs `pip`, `zip`, `unzip` and Docker, which `ubuntu-latest` has. | `requirements-file` | `requirements.txt` | Fully resolved requirements file | | `requirements-command` | | Command printing the requirements, e.g. `pipenv requirements`; used instead of the file when set | | `smoke-test-glob` | `*.py` | Modules (relative to the zip root) to import; build fails if nothing matches | +| `smoke-test-env` | | Newline-separated `NAME=value` variables for the smoke test; `AWS_REGION` and `AWS_DEFAULT_REGION` are always set to `us-east-1`, like in the Lambda runtime | | `output-dir` | `build` | Where to put the zip | | `zip-name` | `package.zip` | File name of the zip | diff --git a/actions/python-lambda-zip/action.yml b/actions/python-lambda-zip/action.yml index 0eef49a..f420c58 100644 --- a/actions/python-lambda-zip/action.yml +++ b/actions/python-lambda-zip/action.yml @@ -43,6 +43,12 @@ inputs: fails if nothing matches. required: false default: '*.py' + smoke-test-env: + description: > + Newline-separated NAME=value environment variables for the smoke test, for code that reads + its configuration when imported. AWS_REGION and AWS_DEFAULT_REGION are always set to + us-east-1, as the Lambda runtime sets them. + required: false output-dir: description: 'Directory to put the zip in' required: false @@ -75,6 +81,7 @@ runs: REQUIREMENTS_FILE: ${{ inputs.requirements-file }} REQUIREMENTS_COMMAND: ${{ inputs.requirements-command }} SMOKE_TEST_GLOB: ${{ inputs.smoke-test-glob }} + SMOKE_TEST_ENV: ${{ inputs.smoke-test-env }} OUTPUT_DIR: ${{ inputs.output-dir }} ZIP_NAME: ${{ inputs.zip-name }} run: ${{ github.action_path }}/build.sh diff --git a/actions/python-lambda-zip/build.sh b/actions/python-lambda-zip/build.sh index 2260333..3654e23 100755 --- a/actions/python-lambda-zip/build.sh +++ b/actions/python-lambda-zip/build.sh @@ -95,8 +95,13 @@ echo "Built $zip_path" mkdir -p "$stage/smoke" unzip -q "$zip_path" -d "$stage/smoke" echo "::group::Smoke test in $image ($docker_platform)" +# The Lambda runtime always sets the region, and code often creates AWS clients at import time +smoke_env=(-e AWS_REGION=us-east-1 -e AWS_DEFAULT_REGION=us-east-1) +while IFS= read -r var; do + [[ -n "$var" ]] && smoke_env+=(-e "$var") +done <<< "${SMOKE_TEST_ENV:-}" docker run --rm --platform "$docker_platform" --entrypoint python \ - -v "$stage/smoke:/var/task:ro" -w /var/task -e GLOB="$SMOKE_TEST_GLOB" "$image" \ + -v "$stage/smoke:/var/task:ro" -w /var/task -e GLOB="$SMOKE_TEST_GLOB" "${smoke_env[@]}" "$image" \ -c "import glob, importlib, os files = glob.glob(os.environ['GLOB'], recursive=True) mods = sorted(f[:-3].replace('/', '.') for f in files if not os.path.basename(f).startswith('_'))