Skip to content

Commit 2a0871c

Browse files
legosciaclaude
andauthored
python-lambda-zip: set AWS region, add smoke-test-env (#30)
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 <noreply@anthropic.com>
1 parent f8e56d8 commit 2a0871c

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎actions/python-lambda-zip/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The runner needs `pip`, `zip`, `unzip` and Docker, which `ubuntu-latest` has.
5050
| `requirements-file` | `requirements.txt` | Fully resolved requirements file |
5151
| `requirements-command` | | Command printing the requirements, e.g. `pipenv requirements`; used instead of the file when set |
5252
| `smoke-test-glob` | `*.py` | Modules (relative to the zip root) to import; build fails if nothing matches |
53+
| `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 |
5354
| `output-dir` | `build` | Where to put the zip |
5455
| `zip-name` | `package.zip` | File name of the zip |
5556

‎actions/python-lambda-zip/action.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ inputs:
4343
fails if nothing matches.
4444
required: false
4545
default: '*.py'
46+
smoke-test-env:
47+
description: >
48+
Newline-separated NAME=value environment variables for the smoke test, for code that reads
49+
its configuration when imported. AWS_REGION and AWS_DEFAULT_REGION are always set to
50+
us-east-1, as the Lambda runtime sets them.
51+
required: false
4652
output-dir:
4753
description: 'Directory to put the zip in'
4854
required: false
@@ -75,6 +81,7 @@ runs:
7581
REQUIREMENTS_FILE: ${{ inputs.requirements-file }}
7682
REQUIREMENTS_COMMAND: ${{ inputs.requirements-command }}
7783
SMOKE_TEST_GLOB: ${{ inputs.smoke-test-glob }}
84+
SMOKE_TEST_ENV: ${{ inputs.smoke-test-env }}
7885
OUTPUT_DIR: ${{ inputs.output-dir }}
7986
ZIP_NAME: ${{ inputs.zip-name }}
8087
run: ${{ github.action_path }}/build.sh

‎actions/python-lambda-zip/build.sh‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,13 @@ echo "Built $zip_path"
9595
mkdir -p "$stage/smoke"
9696
unzip -q "$zip_path" -d "$stage/smoke"
9797
echo "::group::Smoke test in $image ($docker_platform)"
98+
# The Lambda runtime always sets the region, and code often creates AWS clients at import time
99+
smoke_env=(-e AWS_REGION=us-east-1 -e AWS_DEFAULT_REGION=us-east-1)
100+
while IFS= read -r var; do
101+
[[ -n "$var" ]] && smoke_env+=(-e "$var")
102+
done <<< "${SMOKE_TEST_ENV:-}"
98103
docker run --rm --platform "$docker_platform" --entrypoint python \
99-
-v "$stage/smoke:/var/task:ro" -w /var/task -e GLOB="$SMOKE_TEST_GLOB" "$image" \
104+
-v "$stage/smoke:/var/task:ro" -w /var/task -e GLOB="$SMOKE_TEST_GLOB" "${smoke_env[@]}" "$image" \
100105
-c "import glob, importlib, os
101106
files = glob.glob(os.environ['GLOB'], recursive=True)
102107
mods = sorted(f[:-3].replace('/', '.') for f in files if not os.path.basename(f).startswith('_'))

0 commit comments

Comments
 (0)