You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1f9507c
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: aws-python-flask-dynamodb-api/README.md
+28-11Lines changed: 28 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,14 +26,7 @@ This template deploys a [Flask](https://flask.palletsprojects.com/) API on AWS L
26
26
27
27
## Prerequisites
28
28
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.
37
30
-[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).
38
31
- An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials).
39
32
- 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
47
40
serverless deploy
48
41
```
49
42
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
+
50
45
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`.
51
46
52
47
You should see output like this:
53
48
54
49
```text
55
50
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)
57
54
endpoints:
58
55
ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
59
56
ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
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:
140
137
141
138
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`.
142
139
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
+
143
156
`app.py` reads and writes the table with `boto3`:
144
157
145
158
```python
@@ -161,9 +174,11 @@ After the deploy, the API is public and anyone can call it. To restrict access,
161
174
162
175
### Run it locally
163
176
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:
165
178
166
179
```bash
180
+
python3.15 -m venv ./venv
181
+
source ./venv/bin/activate
167
182
pip install werkzeug boto3
168
183
pip install -r requirements.txt
169
184
```
@@ -186,6 +201,8 @@ custom:
186
201
- dev
187
202
```
188
203
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
+
189
206
`app.py` already connects to the local DynamoDB when `serverless-wsgi` sets the `IS_OFFLINE` environment variable:
0 commit comments