From ba9d1d7b9d5b887a1ebd79de9987dd1b42073299 Mon Sep 17 00:00:00 2001 From: Tomasz Czubocha Date: Wed, 25 Nov 2026 22:01:46 +0000 Subject: [PATCH 1/4] fix(aws-python-flask-dynamodb-api): use the built-in Python requirements 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. --- aws-python-flask-dynamodb-api/README.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/aws-python-flask-dynamodb-api/README.md b/aws-python-flask-dynamodb-api/README.md index e5104df3b..c0eecfd00 100644 --- a/aws-python-flask-dynamodb-api/README.md +++ b/aws-python-flask-dynamodb-api/README.md @@ -26,14 +26,7 @@ This template deploys a [Flask](https://flask.palletsprojects.com/) API on AWS L ## Prerequisites -- [Python](https://www.python.org/) 3.14 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. A virtual environment keeps them apart from your other projects: - - ```bash - python3.14 -m venv ./venv - source ./venv/bin/activate - ``` - - To package the dependencies inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`. +- [Python](https://www.python.org/) 3.14 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. To package them inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`. - [Node.js](https://nodejs.org/), to install the Serverless Framework CLI (`npm i -g serverless`) and the `serverless-wsgi` plugin. See [Getting started](https://www.serverless.com/framework/docs/getting-started). - An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials). - A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it. @@ -161,9 +154,11 @@ After the deploy, the API is public and anyone can call it. To restrict access, ### Run it locally -`serverless-wsgi` can serve the application on your machine, against a local DynamoDB. Install `werkzeug`, `boto3` and the requirements first, preferably in a virtual environment: +`serverless-wsgi` can serve the application on your machine, against a local DynamoDB. Install `werkzeug`, `boto3` and the requirements first, in a virtual environment: ```bash +python3.14 -m venv ./venv +source ./venv/bin/activate pip install werkzeug boto3 pip install -r requirements.txt ``` @@ -186,6 +181,16 @@ custom: - dev ``` +The virtual environment and the local DynamoDB files (`.dynamodb/`) are in this directory, so the next `serverless deploy` would add them to the function's package. Leave them out with a top-level `package` section, as [Keep local environments out of the package](https://www.serverless.com/framework/docs/providers/aws/guide/python#keep-local-environments-out-of-the-package) describes: + +```yaml +package: + patterns: + - '!venv/**' + - '!.dynamodb/**' + - '!**/__pycache__/**' +``` + `app.py` already connects to the local DynamoDB when `serverless-wsgi` sets the `IS_OFFLINE` environment variable: ```python From 8d470c246e61ca2adc03f08b039b88ce2018f393 Mon Sep 17 00:00:00 2001 From: Tomasz Czubocha Date: Wed, 25 Nov 2026 22:14:35 +0000 Subject: [PATCH 2/4] feat(aws-python-flask-dynamodb-api): exclude local environments from 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. --- aws-python-flask-dynamodb-api/README.md | 16 ++++------------ aws-python-flask-dynamodb-api/package.json | 2 +- aws-python-flask-dynamodb-api/serverless.yml | 9 ++++++++- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/aws-python-flask-dynamodb-api/README.md b/aws-python-flask-dynamodb-api/README.md index c0eecfd00..55b69efc9 100644 --- a/aws-python-flask-dynamodb-api/README.md +++ b/aws-python-flask-dynamodb-api/README.md @@ -26,7 +26,7 @@ This template deploys a [Flask](https://flask.palletsprojects.com/) API on AWS L ## Prerequisites -- [Python](https://www.python.org/) 3.14 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. To package them inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`. +- [Python](https://www.python.org/) 3.15 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. To package them inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`. - [Node.js](https://nodejs.org/), to install the Serverless Framework CLI (`npm i -g serverless`) and the `serverless-wsgi` plugin. See [Getting started](https://www.serverless.com/framework/docs/getting-started). - An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials). - A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it. @@ -46,7 +46,7 @@ You should see output like this: ```text Deploying "aws-python-flask-dynamodb-api" to stage "dev" (us-east-1) -✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (64s) +✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (66s) endpoints: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/ ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+} @@ -157,7 +157,7 @@ After the deploy, the API is public and anyone can call it. To restrict access, `serverless-wsgi` can serve the application on your machine, against a local DynamoDB. Install `werkzeug`, `boto3` and the requirements first, in a virtual environment: ```bash -python3.14 -m venv ./venv +python3.15 -m venv ./venv source ./venv/bin/activate pip install werkzeug boto3 pip install -r requirements.txt @@ -181,15 +181,7 @@ custom: - dev ``` -The virtual environment and the local DynamoDB files (`.dynamodb/`) are in this directory, so the next `serverless deploy` would add them to the function's package. Leave them out with a top-level `package` section, as [Keep local environments out of the package](https://www.serverless.com/framework/docs/providers/aws/guide/python#keep-local-environments-out-of-the-package) describes: - -```yaml -package: - patterns: - - '!venv/**' - - '!.dynamodb/**' - - '!**/__pycache__/**' -``` +The virtual environment and the local DynamoDB files (`.dynamodb/`) stay out of the deployed package: the `package` section of `serverless.yml` excludes them, as [Keep local environments out of the package](https://www.serverless.com/framework/docs/providers/aws/guide/python#keep-local-environments-out-of-the-package) describes. `app.py` already connects to the local DynamoDB when `serverless-wsgi` sets the `IS_OFFLINE` environment variable: diff --git a/aws-python-flask-dynamodb-api/package.json b/aws-python-flask-dynamodb-api/package.json index e87e35b40..debf32e88 100644 --- a/aws-python-flask-dynamodb-api/package.json +++ b/aws-python-flask-dynamodb-api/package.json @@ -4,6 +4,6 @@ "description": "Example of a Python Flask API service backed by DynamoDB with traditional Serverless Framework", "author": "", "devDependencies": { - "serverless-wsgi": "^3.0.4" + "serverless-wsgi": "3.1.0" } } diff --git a/aws-python-flask-dynamodb-api/serverless.yml b/aws-python-flask-dynamodb-api/serverless.yml index a5a2e266a..302968b78 100644 --- a/aws-python-flask-dynamodb-api/serverless.yml +++ b/aws-python-flask-dynamodb-api/serverless.yml @@ -23,7 +23,7 @@ custom: provider: name: aws - runtime: python3.14 + runtime: python3.15 architecture: arm64 # Uncomment to easily set up a custom domain. Read the docs for more details: # https://www.serverless.com/framework/docs/providers/aws/guide/domains @@ -44,6 +44,13 @@ provider: environment: USERS_TABLE: ${param:tableName} +# Keep a local virtual environment and local DynamoDB files out of the package. +package: + patterns: + - '!venv/**' + - '!.dynamodb/**' + - '!**/__pycache__/**' + functions: api: handler: wsgi_handler.handler From cddb98e9f528f6d03f0c85ce103f7fb9246675f1 Mon Sep 17 00:00:00 2001 From: Tomasz Czubocha Date: Wed, 25 Nov 2026 22:46:19 +0000 Subject: [PATCH 3/4] docs(aws-python-flask-dynamodb-api): show the full deploy output and explain the npm install-script warning --- aws-python-flask-dynamodb-api/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aws-python-flask-dynamodb-api/README.md b/aws-python-flask-dynamodb-api/README.md index 55b69efc9..be58c1f2e 100644 --- a/aws-python-flask-dynamodb-api/README.md +++ b/aws-python-flask-dynamodb-api/README.md @@ -40,12 +40,16 @@ npm install serverless deploy ``` +With npm 12 or later, `npm install` ends with a warning that it blocked the install script of `es5-ext`, a dependency of the plugin. That script only prints a message, so the example needs nothing from it. + Without options, the deploy uses the stage `dev` and the Region `us-east-1`. To change them, pass `--stage` or `--region`, and pass the same options to every later command, including `serverless remove`. You should see output like this: ```text Deploying "aws-python-flask-dynamodb-api" to stage "dev" (us-east-1) +Using Python specified in "runtime": python3.15 +Packaging Python WSGI handler... ✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (66s) endpoints: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/ From a8b057a6ae00605770bd9fef14636c5cc83cdf07 Mon Sep 17 00:00:00 2001 From: Tomasz Czubocha Date: Wed, 25 Nov 2026 23:18:07 +0000 Subject: [PATCH 4/4] fix(aws-python-flask-dynamodb-api): package Linux arm64 wheels on any 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. --- aws-python-flask-dynamodb-api/README.md | 22 +++++++++++++++++--- aws-python-flask-dynamodb-api/serverless.yml | 9 +++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/aws-python-flask-dynamodb-api/README.md b/aws-python-flask-dynamodb-api/README.md index be58c1f2e..f31308ba1 100644 --- a/aws-python-flask-dynamodb-api/README.md +++ b/aws-python-flask-dynamodb-api/README.md @@ -26,7 +26,7 @@ This template deploys a [Flask](https://flask.palletsprojects.com/) API on AWS L ## Prerequisites -- [Python](https://www.python.org/) 3.15 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. To package them inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`. +- [Python](https://www.python.org/) 3.15 on your `PATH`: the deploy runs it to package the dependencies in `requirements.txt`. It downloads Linux arm64 wheels, so the package matches the function on macOS, Windows and Linux alike, without Docker. - [Node.js](https://nodejs.org/), to install the Serverless Framework CLI (`npm i -g serverless`) and the `serverless-wsgi` plugin. See [Getting started](https://www.serverless.com/framework/docs/getting-started). - An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials). - A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it. @@ -50,13 +50,13 @@ You should see output like this: Deploying "aws-python-flask-dynamodb-api" to stage "dev" (us-east-1) Using Python specified in "runtime": python3.15 Packaging Python WSGI handler... -✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (66s) +✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (62s) endpoints: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/ ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+} functions: - api: aws-python-flask-dynamodb-api-dev-api (658 kB) + api: aws-python-flask-dynamodb-api-dev-api (660 kB) ``` The deploy prints the endpoint URLs, which end in the stage name: `https://.execute-api..amazonaws.com/dev/` for the default `dev` stage. @@ -137,6 +137,22 @@ resources: Two `http` events, `/` and `/{proxy+}` with method `ANY`, send every request to the `api` function. [`serverless-wsgi`](https://github.com/logandk/serverless-wsgi) adds the `wsgi_handler.handler` entry point, which turns each Lambda event into a WSGI request for the Flask application named in `custom.wsgi.app`. The Framework's built-in [Python requirements packaging](https://www.serverless.com/framework/docs/providers/aws/guide/python), turned on by `custom.pythonRequirements`, bundles the dependencies in `requirements.txt`. +The function runs on `arm64`, and one of Flask's dependencies, MarkupSafe, includes compiled code. `pipCmdExtraArgs` tells pip to download wheels built for Linux on arm64 and Python 3.15, whatever machine you deploy from, as [Linux wheels without Docker](https://www.serverless.com/framework/docs/providers/aws/guide/python#linux-wheels-without-docker) describes: + +```yaml +custom: + pythonRequirements: + # Download Linux arm64 wheels for Python 3.15 on any machine, so compiled + # dependencies (such as MarkupSafe) match the Lambda architecture and runtime. + pipCmdExtraArgs: + - '--platform=manylinux2014_aarch64' + - '--platform=manylinux_2_28_aarch64' + - '--python-version=3.15' + - '--only-binary=:all:' +``` + +With `--only-binary=:all:`, a dependency you add that publishes no such wheel stops the deploy instead of being built for your machine. To build those inside a Docker container, set the `dockerizePip` option under `custom.pythonRequirements`. + `app.py` reads and writes the table with `boto3`: ```python diff --git a/aws-python-flask-dynamodb-api/serverless.yml b/aws-python-flask-dynamodb-api/serverless.yml index 302968b78..e34333fc1 100644 --- a/aws-python-flask-dynamodb-api/serverless.yml +++ b/aws-python-flask-dynamodb-api/serverless.yml @@ -19,7 +19,14 @@ custom: # built-in python requirements packaging (activated by custom.pythonRequirements below). packRequirements: false # Built-in python requirements packaging activates on this key. - pythonRequirements: {} + pythonRequirements: + # Download Linux arm64 wheels for Python 3.15 on any machine, so compiled + # dependencies (such as MarkupSafe) match the Lambda architecture and runtime. + pipCmdExtraArgs: + - '--platform=manylinux2014_aarch64' + - '--platform=manylinux_2_28_aarch64' + - '--python-version=3.15' + - '--only-binary=:all:' provider: name: aws