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
Copy file name to clipboardExpand all lines: aws-python-flask-api/README.md
+26-59
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,19 @@
1
1
<!--
2
2
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.'
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.
18
17
19
18
## Anatomy of the template
20
19
@@ -26,7 +25,7 @@ This template configures a single function, `api`, which is responsible for hand
26
25
27
26
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:
28
27
29
-
```bash
28
+
```
30
29
python3.8 -m venv ./venv
31
30
source ./venv/bin/activate
32
31
```
@@ -63,41 +62,21 @@ serverless deploy
63
62
64
63
After running deploy, you should see output similar to:
65
64
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
+
94
74
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+}
97
77
functions:
98
-
api: aws-python-flask-api-dev-api
99
-
layers:
100
-
None
78
+
api: aws-python-flask-api-dev-api (41 MB)
79
+
101
80
```
102
81
103
82
_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
106
85
107
86
After successful deployment, you can call the created application via HTTP:
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:
146
113
147
-
```bash
114
+
```
148
115
pip install werkzeug
149
116
pip install -r requirements.txt
150
117
```
151
118
152
119
At this point, you can run your application locally with the following command:
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.'
# Serverless Framework Python Flask API service backed by DynamoDB on AWS
15
15
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.
20
17
21
18
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).
22
19
@@ -28,7 +25,7 @@ Additionally, the template also handles provisioning of a DynamoDB database that
28
25
29
26
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:
30
27
31
-
```bash
28
+
```
32
29
python3.8 -m venv ./venv
33
30
source ./venv/bin/activate
34
31
```
@@ -37,14 +34,6 @@ Alternatively, you can also use `dockerizePip` configuration from `serverless-py
37
34
38
35
### Deployment
39
36
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
-
48
37
install dependencies with:
49
38
50
39
```
@@ -59,41 +48,20 @@ serverless deploy
59
48
60
49
After running deploy, you should see output similar to:
61
50
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
+
90
60
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+}
_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
102
70
103
71
After successful deployment, you can create a new user by calling the corresponding endpoint:
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" }
131
93
```
132
94
133
95
### Local development
134
96
135
97
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:
136
98
137
-
```bash
99
+
```
138
100
pip install werkzeug boto3
139
101
pip install -r requirements.txt
140
102
```
141
103
142
104
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:
@@ -150,7 +112,6 @@ It will add the plugin to `devDependencies` in `package.json` file as well as to
150
112
151
113
You should also add the following config to `custom` section in `serverless.yml`:
152
114
153
-
154
115
```yml
155
116
custom:
156
117
(...)
@@ -163,7 +124,6 @@ custom:
163
124
164
125
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:
165
126
166
-
167
127
```python
168
128
dynamodb_client = boto3.client('dynamodb')
169
129
```
@@ -179,16 +139,17 @@ if os.environ.get('IS_OFFLINE'):
179
139
180
140
Now you can start DynamoDB local with the following command:
181
141
182
-
```bash
142
+
```
183
143
serverless dynamodb start
184
144
```
185
145
186
146
At this point, you can run your application locally with the following command:
187
147
188
-
```bash
148
+
```
189
149
serverless wsgi serve
190
150
```
191
151
192
152
For additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories:
0 commit comments