Skip to content

Commit bcd176f

Browse files
committed
updating onboarding AWS Python examples
1 parent 5608135 commit bcd176f

21 files changed

+117
-360
lines changed

aws-python-flask-api/README.md

+26-59
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
<!--
22
title: 'Serverless Framework Python Flask API on AWS'
3-
description: 'This template demonstrates how to develop and deploy a simple Python Flask API running on AWS Lambda using the traditional Serverless Framework.'
3+
description: 'This template demonstrates how to develop and deploy a simple Python Flask API running on AWS Lambda using the Serverless Framework.'
44
layout: Doc
5-
framework: v2
5+
framework: v4
66
platform: AWS
77
language: Python
88
priority: 2
99
authorLink: 'https://github.com/serverless'
10-
authorName: 'Serverless, inc.'
10+
authorName: 'Serverless, Inc.'
1111
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
1212
-->
1313

1414
# Serverless Framework Python Flask API on AWS
1515

16-
This template demonstrates how to develop and deploy a simple Python Flask API service running on AWS Lambda using the traditional Serverless Framework.
17-
16+
This template demonstrates how to develop and deploy a simple Python Flask API service running on AWS Lambda using the Serverless Framework.
1817

1918
## Anatomy of the template
2019

@@ -26,7 +25,7 @@ This template configures a single function, `api`, which is responsible for hand
2625

2726
In order to package your dependencies locally with `serverless-python-requirements`, you need to have `Python3.8` installed locally. You can create and activate a dedicated virtual environment with the following command:
2827

29-
```bash
28+
```
3029
python3.8 -m venv ./venv
3130
source ./venv/bin/activate
3231
```
@@ -63,41 +62,21 @@ serverless deploy
6362

6463
After running deploy, you should see output similar to:
6564

66-
```bash
67-
erverless: Using Python specified in "runtime": python3.8
68-
Serverless: Packaging Python WSGI handler...
69-
Serverless: Generated requirements from /home/xxx/xxx/xxx/examples/aws-python-flask-api/requirements.txt in /home/xxx/xxx/xxx/examples/aws-python-flask-api/.serverless/requirements.txt...
70-
Serverless: Using static cache of requirements found at /home/xxx/.cache/serverless-python-requirements/62f10436f9a1bb8040df30ef2db5736c8015b18256bf0b6f1b0cbb2640030244_slspyc ...
71-
Serverless: Packaging service...
72-
Serverless: Excluding development dependencies...
73-
Serverless: Injecting required Python packages to package...
74-
Serverless: Creating Stack...
75-
Serverless: Checking Stack create progress...
76-
........
77-
Serverless: Stack create finished...
78-
Serverless: Uploading CloudFormation file to S3...
79-
Serverless: Uploading artifacts...
80-
Serverless: Uploading service aws-python-flask-api.zip file to S3 (1.3 MB)...
81-
Serverless: Validating template...
82-
Serverless: Updating Stack...
83-
Serverless: Checking Stack update progress...
84-
.................................
85-
Serverless: Stack update finished...
86-
Service Information
87-
service: aws-python-flask-api
88-
stage: dev
89-
region: us-east-1
90-
stack: aws-python-flask-api-dev
91-
resources: 12
92-
api keys:
93-
None
65+
```
66+
Deploying "aws-python-flask-api" to stage "dev" (us-east-1)
67+
68+
Using Python specified in "runtime": python3.12
69+
70+
Packaging Python WSGI handler...
71+
72+
✔ Service deployed to stack aws-python-flask-api-dev (104s)
73+
9474
endpoints:
95-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
96-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
75+
ANY - https://ce9zsxqh6e.execute-api.us-east-1.amazonaws.com/dev/
76+
ANY - https://ce9zsxqh6e.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
9777
functions:
98-
api: aws-python-flask-api-dev-api
99-
layers:
100-
None
78+
api: aws-python-flask-api-dev-api (41 MB)
79+
10180
```
10281

10382
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/).
@@ -106,52 +85,40 @@ _Note_: In current form, after deployment, your API is public and can be invoked
10685

10786
After successful deployment, you can call the created application via HTTP:
10887

109-
```bash
88+
```
11089
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
11190
```
11291

11392
Which should result in the following response:
11493

115-
```
116-
{"message":"Hello from root!"}
94+
```json
95+
{ "message": "Hello from root!" }
11796
```
11897

11998
Calling the `/hello` path with:
12099

121-
```bash
100+
```
122101
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
123102
```
124103

125104
Should result in the following response:
126105

127-
```bash
128-
{"message":"Hello from path!"}
129-
```
130-
131-
If you try to invoke a path or method that does not have a configured handler, e.g. with:
132-
133-
```bash
134-
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent
135-
```
136-
137-
You should receive the following response:
138-
139-
```bash
140-
{"error":"Not Found!"}
106+
```json
107+
{ "message": "Hello from path!" }
141108
```
142109

143110
### Local development
144111

145112
Thanks to capabilities of `serverless-wsgi`, it is also possible to run your application locally, however, in order to do that, you will need to first install `werkzeug` dependency, as well as all other dependencies listed in `requirements.txt`. It is recommended to use a dedicated virtual environment for that purpose. You can install all needed dependencies with the following commands:
146113

147-
```bash
114+
```
148115
pip install werkzeug
149116
pip install -r requirements.txt
150117
```
151118

152119
At this point, you can run your application locally with the following command:
153120

154-
```bash
121+
```
155122
serverless wsgi serve
156123
```
157124

aws-python-flask-api/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Example of a Python Flask API service with traditional Serverless Framework",
55
"author": "",
66
"devDependencies": {
7-
"serverless-python-requirements": "^5.1.0",
8-
"serverless-wsgi": "^1.7.6"
7+
"serverless-python-requirements": "^6.1.0",
8+
"serverless-wsgi": "^3.0.4"
99
}
1010
}

aws-python-flask-api/requirements.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Flask==1.1.4
2-
Werkzeug==1.0.1
3-
markupsafe==2.0.1
1+
Flask==3.0.3

aws-python-flask-api/serverless.template.yml

-6
This file was deleted.

aws-python-flask-api/serverless.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
service: aws-python-flask-api
22

3-
frameworkVersion: '4'
3+
frameworkVersion: "4"
44

55
custom:
66
wsgi:
@@ -9,6 +9,7 @@ custom:
99
provider:
1010
name: aws
1111
runtime: python3.12
12+
1213
functions:
1314
api:
1415
handler: wsgi_handler.handler
+29-68
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
<!--
22
title: 'Serverless Framework Python Flask API backed by DynamoDB on AWS'
3-
description: 'This template demonstrates how to develop and deploy a simple Python Flask API service backed by DynamoDB running on AWS Lambda using the traditional Serverless Framework.'
3+
description: 'This template demonstrates how to develop and deploy a simple Python Flask API service backed by DynamoDB running on AWS Lambda using the Serverless Framework.'
44
layout: Doc
5-
framework: v2
5+
framework: v4
66
platform: AWS
77
language: Python
88
priority: 2
99
authorLink: 'https://github.com/serverless'
10-
authorName: 'Serverless, inc.'
10+
authorName: 'Serverless, Inc.'
1111
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
1212
-->
1313

1414
# Serverless Framework Python Flask API service backed by DynamoDB on AWS
1515

16-
This template demonstrates how to develop and deploy a simple Python Flask API service, backed by DynamoDB, running on AWS Lambda using the traditional Serverless Framework.
17-
18-
19-
## Anatomy of the template
16+
This template demonstrates how to develop and deploy a simple Python Flask API service, backed by DynamoDB, running on AWS Lambda using the Serverless Framework.
2017

2118
This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to configured `http` events. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). As the events are configured in a way to accept all incoming requests, `Flask` framework is responsible for routing and handling requests internally. The implementation takes advantage of `serverless-wsgi`, which allows you to wrap WSGI applications such as Flask apps. To learn more about `serverless-wsgi`, please refer to corresponding [GitHub repository](https://github.com/logandk/serverless-wsgi). The template also relies on `serverless-python-requirements` plugin for packaging dependencies from `requirements.txt` file. For more details about `serverless-python-requirements` configuration, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).
2219

@@ -28,7 +25,7 @@ Additionally, the template also handles provisioning of a DynamoDB database that
2825

2926
In order to package your dependencies locally with `serverless-python-requirements`, you need to have `Python3.8` installed locally. You can create and activate a dedicated virtual environment with the following command:
3027

31-
```bash
28+
```
3229
python3.8 -m venv ./venv
3330
source ./venv/bin/activate
3431
```
@@ -37,14 +34,6 @@ Alternatively, you can also use `dockerizePip` configuration from `serverless-py
3734

3835
### Deployment
3936

40-
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
41-
42-
In order to deploy with dashboard, you need to first login with:
43-
44-
```
45-
serverless login
46-
```
47-
4837
install dependencies with:
4938

5039
```
@@ -59,41 +48,20 @@ serverless deploy
5948

6049
After running deploy, you should see output similar to:
6150

62-
```bash
63-
Serverless: Using Python specified in "runtime": python3.8
64-
Serverless: Packaging Python WSGI handler...
65-
Serverless: Generated requirements from /home/xxx/xxx/xxx/examples/aws-python-flask-dynamodb-api/requirements.txt in /home/xxx/xxx/xxx/examples/aws-python-flask-dynamodb-api/.serverless/requirements.txt...
66-
Serverless: Using static cache of requirements found at /home/xxx/.cache/serverless-python-requirements/62f10436f9a1bb8040df30ef2db5736c8015b18256bf0b6f1b0cbb2640030244_slspyc ...
67-
Serverless: Packaging service...
68-
Serverless: Excluding development dependencies...
69-
Serverless: Injecting required Python packages to package...
70-
Serverless: Creating Stack...
71-
Serverless: Checking Stack create progress...
72-
........
73-
Serverless: Stack create finished...
74-
Serverless: Uploading CloudFormation file to S3...
75-
Serverless: Uploading artifacts...
76-
Serverless: Uploading service aws-python-flask-dynamodb-api.zip file to S3 (1.3 MB)...
77-
Serverless: Validating template...
78-
Serverless: Updating Stack...
79-
Serverless: Checking Stack update progress...
80-
.................................
81-
Serverless: Stack update finished...
82-
Service Information
83-
service: aws-python-flask-dynamodb-api
84-
stage: dev
85-
region: us-east-1
86-
stack: aws-python-flask-dynamodb-api-dev
87-
resources: 12
88-
api keys:
89-
None
51+
```
52+
Deploying "aws-python-flask-dynamodb-api" to stage "dev" (us-east-1)
53+
54+
Using Python specified in "runtime": python3.12
55+
56+
Packaging Python WSGI handler...
57+
58+
✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (123s)
59+
9060
endpoints:
91-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
92-
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
61+
ANY - https://r9a04q5it7.execute-api.us-east-1.amazonaws.com/dev/
62+
ANY - https://r9a04q5it7.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
9363
functions:
94-
api: aws-python-flask-dynamodb-api-dev-api
95-
layers:
96-
None
64+
api: aws-python-flask-dynamodb-api-dev-api (41 MB)
9765
```
9866

9967
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/).
@@ -102,46 +70,40 @@ _Note_: In current form, after deployment, your API is public and can be invoked
10270

10371
After successful deployment, you can create a new user by calling the corresponding endpoint:
10472

105-
```bash
73+
```
10674
curl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/dev/users' --header 'Content-Type: application/json' --data-raw '{"name": "John", "userId": "someUserId"}'
10775
```
10876

10977
Which should result in the following response:
11078

111-
```bash
112-
{"userId":"someUserId","name":"John"}
79+
```json
80+
{ "userId": "someUserId", "name": "John" }
11381
```
11482

11583
You can later retrieve the user by `userId` by calling the following endpoint:
11684

117-
```bash
85+
```
11886
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users/someUserId
11987
```
12088

12189
Which should result in the following response:
12290

123-
```bash
124-
{"userId":"someUserId","name":"John"}
125-
```
126-
127-
If you try to retrieve user that does not exist, you should receive the following response:
128-
129-
```bash
130-
{"error":"Could not find user with provided \"userId\""}
91+
```json
92+
{ "userId": "someUserId", "name": "John" }
13193
```
13294

13395
### Local development
13496

13597
Thanks to capabilities of `serverless-wsgi`, it is also possible to run your application locally, however, in order to do that, you will need to first install `werkzeug`, `boto3` dependencies, as well as all other dependencies listed in `requirements.txt`. It is recommended to use a dedicated virtual environment for that purpose. You can install all needed dependencies with the following commands:
13698

137-
```bash
99+
```
138100
pip install werkzeug boto3
139101
pip install -r requirements.txt
140102
```
141103

142104
Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb-local` plugin. In order to do that, execute the following commands:
143105

144-
```bash
106+
```
145107
serverless plugin install -n serverless-dynamodb-local
146108
serverless dynamodb install
147109
```
@@ -150,7 +112,6 @@ It will add the plugin to `devDependencies` in `package.json` file as well as to
150112

151113
You should also add the following config to `custom` section in `serverless.yml`:
152114

153-
154115
```yml
155116
custom:
156117
(...)
@@ -163,7 +124,6 @@ custom:
163124
164125
Additionally, we need to reconfigure DynamoDB Client to connect to our local instance of DynamoDB. We can take advantage of `IS_OFFLINE` environment variable set by `serverless-wsgi` plugin and replace:
165126

166-
167127
```python
168128
dynamodb_client = boto3.client('dynamodb')
169129
```
@@ -179,16 +139,17 @@ if os.environ.get('IS_OFFLINE'):
179139

180140
Now you can start DynamoDB local with the following command:
181141

182-
```bash
142+
```
183143
serverless dynamodb start
184144
```
185145

186146
At this point, you can run your application locally with the following command:
187147

188-
```bash
148+
```
189149
serverless wsgi serve
190150
```
191151

192152
For additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories:
193-
- https://github.com/logandk/serverless-wsgi
153+
154+
- https://github.com/logandk/serverless-wsgi
194155
- https://github.com/99x/serverless-dynamodb-local

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Example of a Python Flask API service backed by DynamoDB with traditional Serverless Framework",
55
"author": "",
66
"devDependencies": {
7-
"serverless-python-requirements": "^6.0.1",
8-
"serverless-wsgi": "^3.0.3"
7+
"serverless-python-requirements": "^6.1.0",
8+
"serverless-wsgi": "^3.0.4"
99
}
1010
}

0 commit comments

Comments
 (0)