Skip to content

Commit 1f9507c

Browse files
authored
fix(aws-python-flask-dynamodb-api): use python3.15, keep local environments out of the package and package arm64 wheels (#844)
1 parent 327ea3a commit 1f9507c

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎aws-python-flask-dynamodb-api/README.md‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ This template deploys a [Flask](https://flask.palletsprojects.com/) API on AWS L
2626

2727
## Prerequisites
2828

29-
- [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:
30-
31-
```bash
32-
python3.14 -m venv ./venv
33-
source ./venv/bin/activate
34-
```
35-
36-
To package the dependencies inside a Docker container instead, set the `dockerizePip` option under `custom.pythonRequirements` in `serverless.yml`.
29+
- [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.
3730
- [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).
3831
- An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials).
3932
- A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it.
@@ -47,19 +40,23 @@ npm install
4740
serverless deploy
4841
```
4942

43+
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.
44+
5045
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`.
5146

5247
You should see output like this:
5348

5449
```text
5550
Deploying "aws-python-flask-dynamodb-api" to stage "dev" (us-east-1)
56-
✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (64s)
51+
Using Python specified in "runtime": python3.15
52+
Packaging Python WSGI handler...
53+
✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (62s)
5754
endpoints:
5855
ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
5956
ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
6057
6158
functions:
62-
api: aws-python-flask-dynamodb-api-dev-api (658 kB)
59+
api: aws-python-flask-dynamodb-api-dev-api (660 kB)
6360
```
6461

6562
The deploy prints the endpoint URLs, which end in the stage name: `https://<api-id>.execute-api.<region>.amazonaws.com/dev/` for the default `dev` stage.
@@ -140,6 +137,22 @@ resources:
140137
141138
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`.
142139

140+
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:
141+
142+
```yaml
143+
custom:
144+
pythonRequirements:
145+
# Download Linux arm64 wheels for Python 3.15 on any machine, so compiled
146+
# dependencies (such as MarkupSafe) match the Lambda architecture and runtime.
147+
pipCmdExtraArgs:
148+
- '--platform=manylinux2014_aarch64'
149+
- '--platform=manylinux_2_28_aarch64'
150+
- '--python-version=3.15'
151+
- '--only-binary=:all:'
152+
```
153+
154+
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`.
155+
143156
`app.py` reads and writes the table with `boto3`:
144157

145158
```python
@@ -161,9 +174,11 @@ After the deploy, the API is public and anyone can call it. To restrict access,
161174

162175
### Run it locally
163176

164-
`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:
177+
`serverless-wsgi` can serve the application on your machine, against a local DynamoDB. Install `werkzeug`, `boto3` and the requirements first, in a virtual environment:
165178

166179
```bash
180+
python3.15 -m venv ./venv
181+
source ./venv/bin/activate
167182
pip install werkzeug boto3
168183
pip install -r requirements.txt
169184
```
@@ -186,6 +201,8 @@ custom:
186201
- dev
187202
```
188203

204+
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.
205+
189206
`app.py` already connects to the local DynamoDB when `serverless-wsgi` sets the `IS_OFFLINE` environment variable:
190207

191208
```python

‎aws-python-flask-dynamodb-api/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"description": "Example of a Python Flask API service backed by DynamoDB with traditional Serverless Framework",
55
"author": "",
66
"devDependencies": {
7-
"serverless-wsgi": "^3.0.4"
7+
"serverless-wsgi": "3.1.0"
88
}
99
}

‎aws-python-flask-dynamodb-api/serverless.yml‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ custom:
1919
# built-in python requirements packaging (activated by custom.pythonRequirements below).
2020
packRequirements: false
2121
# Built-in python requirements packaging activates on this key.
22-
pythonRequirements: {}
22+
pythonRequirements:
23+
# Download Linux arm64 wheels for Python 3.15 on any machine, so compiled
24+
# dependencies (such as MarkupSafe) match the Lambda architecture and runtime.
25+
pipCmdExtraArgs:
26+
- '--platform=manylinux2014_aarch64'
27+
- '--platform=manylinux_2_28_aarch64'
28+
- '--python-version=3.15'
29+
- '--only-binary=:all:'
2330

2431
provider:
2532
name: aws
26-
runtime: python3.14
33+
runtime: python3.15
2734
architecture: arm64
2835
# Uncomment to easily set up a custom domain. Read the docs for more details:
2936
# https://www.serverless.com/framework/docs/providers/aws/guide/domains
@@ -44,6 +51,13 @@ provider:
4451
environment:
4552
USERS_TABLE: ${param:tableName}
4653

54+
# Keep a local virtual environment and local DynamoDB files out of the package.
55+
package:
56+
patterns:
57+
- '!venv/**'
58+
- '!.dynamodb/**'
59+
- '!**/__pycache__/**'
60+
4761
functions:
4862
api:
4963
handler: wsgi_handler.handler

0 commit comments

Comments
 (0)