Skip to content

fix(aws-python-flask-dynamodb-api): use python3.15, keep local environments out of the package and package arm64 wheels - #844

Merged
czubocha merged 7 commits into
v4from
branch/dreamy-lamport-709whe-flask
Nov 26, 2026
Merged

czubocha merged 7 commits into
v4from
branch/dreamy-lamport-709whe-flask

Conversation

@czubocha

Copy link
Copy Markdown
Contributor

Lesson

aws-python-flask-dynamodb-api teaches a Flask API on Lambda that stores users in DynamoDB. Its dependencies are packaged by the Framework's built-in Python requirements packaging (custom.pythonRequirements), with no serverless-python-requirements plugin. serverless.yml and package.json already made that switch on v4 in #827; this pull request brings the README in line with it, keeps local environments out of the package, moves the example to the python3.15 runtime, packages Linux arm64 wheels on any machine, and adds the proof.

Left out: everything the example didn't already teach. requirements.txt and app.py are unchanged.

Changes

Onboarding example. Files touched: README.md, serverless.yml and package.json.

  • serverless.yml has a top-level package.patterns that leaves out venv/, .dynamodb/ (what serverless dynamodb install downloads) and __pycache__/, so a reader who follows Run it locally never deploys their local environment. With those directories present, serverless package built a 200-file, 2.3 MB (unzipped) package with none of them in it; without the patterns, the same steps had built a 165 MB zip.
  • serverless.yml uses the python3.15 runtime (Framework CLI 4.47.0 accepts it without a configuration warning; it deployed and served requests).
  • package.json pins serverless-wsgi to 3.1.0. npm audit: 0 vulnerabilities.
  • serverless.yml passes custom.pythonRequirements.pipCmdExtraArgs (--platform=manylinux2014_aarch64, --platform=manylinux_2_28_aarch64, --python-version=3.15, '--only-binary=:all:'), with a comment saying why, as Linux wheels without Docker describes. The function runs on arm64, and pip otherwise installs wheels for the machine that packages it: packaged on an x86_64 host, the zip held markupsafe/_speedups.cpython-315-x86_64-linux-gnu.so, which the function can't load (MarkupSafe then uses its pure-Python code). The Framework adds --target itself, and pip's --implementation defaults to CPython, the interpreter that runs it, so neither is set.
  • README Prerequisites asks for Python 3.15 and no longer asks for a virtual environment in the service directory: the built-in packaging runs pip itself, and a venv/ there used to be packaged into the function (a 65 MB package, of which 64 MB was venv/).
  • README Prerequisites and How it works say that the deploy downloads Linux arm64 wheels on any machine, quote the pipCmdExtraArgs block, and name dockerizePip for a dependency that publishes no such wheel.
  • README Run it locally creates the virtual environment with python3.15 and says that the package section of serverless.yml keeps it and .dynamodb/ out of the package, linking to Keep local environments out of the package.
  • README Deploy output is from the last live run (62 s, 660 kB).

Test plan

Framework CLI 4.47.0, us-east-1.

  • Static: node scripts/check-examples.mjs aws-python-flask-dynamodb-api clean; npm test 323 passed; npm run validate clean; npm run docs and npm run check-docs clean (after merging the current v4).
  • Package contents on an x86_64 host (serverless package): before the change, the one compiled file was markupsafe/_speedups.cpython-315-x86_64-linux-gnu.so. After it, the one compiled file is markupsafe/_speedups.cpython-315-aarch64-linux-gnu.so (file: ELF 64-bit LSB shared object, ARM aarch64), MarkupSafe's wheel tag is cp315-cp315-manylinux2014_aarch64, every other package is py3-none-any, and no file name in the zip contains x86_64.
  • In the deployed function, serverless wsgi exec imported markupsafe._speedups from /var/task/markupsafe/_speedups.cpython-315-aarch64-linux-gnu.so with platform.machine() = aarch64.
  • Live test of the example with these changes:
    PASS aws-python-flask-dynamodb-api 4.47.0 119s
    
    (stage exqs3ee456k; Using Python specified in "runtime": python3.15; created and read back user smoke-exqs3ee456k-…; package 660 kB)
  • Negative control: a scratch copy whose smoke script expected a different name stopped at the check step, FAIL zz-neg-flask 4.47.0 117s (stage exnn9bl6qe6).
  • README followed by hand from a clean copy with the arm64 wheels change (stage flkarm1125): Deploy printed the README's lines and the 660 kB package; both Try it requests returned {"name":"John","userId":"someUserId"} with status 200, and an unknown id answered 404 with the README's error. The function's configuration reads arm64, python3.15.
  • Run it locally (earlier run, python3.14): the steps created users-table-dev and served the same responses on localhost:5000.
  • Fresh-agent test of the final README: a coding agent with no other context, given only the README, deployed the example (stage faeu842), ran both Try it requests and the unknown-id request, and removed it. It reported no step where it had to guess or look anything up. Deploy printed the README's lines and the 660 kB package (67 s); all three responses matched the README exactly; serverless remove succeeded and describe-stacks then reported that the stack does not exist. The only extra output was a Node.js DEP0169 (url.parse()) deprecation warning on Node.js 26.
  • Leftovers after the last run: aws cloudformation list-stacks shows no aws-python-flask-dynamodb-api stack outside DELETE_COMPLETE, aws logs describe-log-groups --log-group-name-prefix /aws/lambda/aws-python-flask-dynamodb-api returns 0 groups, and no users-table-* table remains.

Generated by Claude Code

…nts packaging

Drop the virtual environment from Prerequisites: the built-in packaging runs python3.14 itself, and a venv in the service directory is packaged into the function. Create it in Run it locally instead, and leave it, the local DynamoDB files and __pycache__ out of the package with package.patterns.
…the package, use python3.15

Approved changes to this onboarding example:

- serverless.yml excludes venv/, .dynamodb/ and __pycache__/ from the
  package, so a local virtual environment or local DynamoDB never reaches
  the deployed function; the README now points to that section instead of
  asking readers to add it.
- Pin serverless-wsgi to 3.1.0.
- Use the python3.15 runtime.
… machine

The function runs on arm64, but pip installed wheels for the machine that
packaged it, so a deploy from an x86_64 host bundled an x86_64 build of
MarkupSafe's compiled module. Pass pip the Linux aarch64 platform tags, the
runtime's Python version and --only-binary=:all: through
custom.pythonRequirements.pipCmdExtraArgs, and explain the setting in the
README.
@czubocha
czubocha merged commit 1f9507c into v4 Nov 26, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant