Skip to content

Commit bf5a5ff

Browse files
committed
feat: Add aws-python-flask-api example
1 parent 3fb63db commit bf5a5ff

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
107107
| [Aws Nodejs Websockets Authorizers](https://github.com/serverless/examples/tree/master/aws-node-websockets-authorizers) <br/> Simple example that demonstrates how to use authorizer functions with websocket events | nodeJS |
108108
| [Aws Alexa Skill](https://github.com/serverless/examples/tree/master/aws-python-alexa-skill) <br/> This example demonstrates how to use an AWS Lambdas for your custom Alexa skill. | python |
109109
| [Aws Auth0 Api Gateway](https://github.com/serverless/examples/tree/master/aws-python-auth0-custom-authorizers-api) <br/> Demonstration of protecting API gateway endpoints with auth0 | python |
110+
| [Aws Python Flask Api](https://github.com/serverless/examples/tree/master/aws-python-flask-api) <br/> Example of a Python Flask API service with traditional Serverless Framework | python |
110111
| [Aws Python Line Echo Bot](https://github.com/serverless/examples/tree/master/aws-python-line-echo-bot) <br/> this is echo bot on LINE message | python |
111112
| [Aws Python Pynamodb S3 Sigurl](https://github.com/serverless/examples/tree/master/aws-python-pynamodb-s3-sigurl) <br/> Serverless signed uploader REST API using pynamodb, s3 generated events, custom log format, and DRY serverless.yml with custom section | python |
112113
| [Aws Rest With Dynamodb](https://github.com/serverless/examples/tree/master/aws-python-rest-api-with-dynamodb) <br/> Serverless CRUD service exposing a REST HTTP interface | python |

aws-python-flask-api/README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!--
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.'
4+
layout: Doc
5+
framework: v2
6+
platform: AWS
7+
language: Python
8+
authorLink: 'https://github.com/serverless'
9+
authorName: 'Serverless, inc.'
10+
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
11+
-->
12+
13+
# Serverless Framework Python Flask API on AWS
14+
15+
This template demonstrates how to develop and deploy a simple Python Flask API service running on AWS Lambda using the traditional Serverless Framework.
16+
17+
18+
## Anatomy of the template
19+
20+
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). Additionally, the template 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).
21+
22+
## Usage
23+
24+
### Prerequisites
25+
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:
27+
28+
```bash
29+
python3.8 -m venv ./venv
30+
source ./venv/bin/activate
31+
```
32+
33+
Alternatively, you can also use `dockerizePip` configuration from `serverless-python-requirements`. For details on that, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).
34+
35+
### Deployment
36+
37+
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
38+
39+
In order to deploy with dashboard, you need to first login with:
40+
41+
```
42+
serverless login
43+
```
44+
45+
install dependencies with:
46+
47+
```
48+
npm install
49+
```
50+
51+
and then perform deployment with:
52+
53+
```
54+
serverless deploy
55+
```
56+
57+
After running deploy, you should see output similar to:
58+
59+
```bash
60+
erverless: Using Python specified in "runtime": python3.8
61+
Serverless: Packaging Python WSGI handler...
62+
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...
63+
Serverless: Using static cache of requirements found at /home/xxx/.cache/serverless-python-requirements/62f10436f9a1bb8040df30ef2db5736c8015b18256bf0b6f1b0cbb2640030244_slspyc ...
64+
Serverless: Packaging service...
65+
Serverless: Excluding development dependencies...
66+
Serverless: Injecting required Python packages to package...
67+
Serverless: Creating Stack...
68+
Serverless: Checking Stack create progress...
69+
........
70+
Serverless: Stack create finished...
71+
Serverless: Uploading CloudFormation file to S3...
72+
Serverless: Uploading artifacts...
73+
Serverless: Uploading service aws-python-flask-api.zip file to S3 (1.3 MB)...
74+
Serverless: Validating template...
75+
Serverless: Updating Stack...
76+
Serverless: Checking Stack update progress...
77+
.................................
78+
Serverless: Stack update finished...
79+
Service Information
80+
service: aws-python-flask-api
81+
stage: dev
82+
region: us-east-1
83+
stack: aws-python-flask-api-dev
84+
resources: 12
85+
api keys:
86+
None
87+
endpoints:
88+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
89+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
90+
functions:
91+
api: aws-python-flask-api-dev-api
92+
layers:
93+
None
94+
```
95+
96+
_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/).
97+
98+
### Invocation
99+
100+
After successful deployment, you can call the created application via HTTP:
101+
102+
```bash
103+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
104+
```
105+
106+
Which should result in the following response:
107+
108+
```
109+
{"message":"Hello from root!"}
110+
```
111+
112+
Calling the `/hello` path with:
113+
114+
```bash
115+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
116+
```
117+
118+
Should result in the following response:
119+
120+
```bash
121+
{"message":"Hello from path!"}
122+
```
123+
124+
If you try to invoke a path or method that does not have a configured handler, e.g. with:
125+
126+
```bash
127+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent
128+
```
129+
130+
You should receive the following response:
131+
132+
```bash
133+
{"error":"Not Found!"}
134+
```
135+
136+
### Local development
137+
138+
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:
139+
140+
```bash
141+
pip install werkzeug
142+
pip install -r requirements.txt
143+
```
144+
145+
At this point, you can run your application locally with the following command:
146+
147+
```bash
148+
serverless wsgi serve
149+
```
150+
151+
For additional local development capabilities of `serverless-wsgi` plugin, please refer to corresponding [GitHub repository](https://github.com/logandk/serverless-wsgi).

aws-python-flask-api/app.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from flask import Flask, jsonify, make_response
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route("/")
7+
def hello_from_root():
8+
return jsonify(message='Hello from root!')
9+
10+
11+
@app.route("/hello")
12+
def hello():
13+
return jsonify(message='Hello from path!')
14+
15+
16+
@app.errorhandler(404)
17+
def resource_not_found(e):
18+
return make_response(jsonify(error='Not found!'), 404)

aws-python-flask-api/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "aws-python-flask-api",
3+
"version": "1.0.0",
4+
"description": "Example of a Python Flask API service with traditional Serverless Framework",
5+
"author": "",
6+
"devDependencies": {
7+
"serverless-python-requirements": "^5.1.0",
8+
"serverless-wsgi": "^1.7.6"
9+
}
10+
}

aws-python-flask-api/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==1.1.2

aws-python-flask-api/serverless.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
service: aws-python-flask-api
2+
3+
frameworkVersion: '2'
4+
variablesResolutionMode: '20210219'
5+
configValidationMode: 'error'
6+
7+
custom:
8+
wsgi:
9+
app: app.app
10+
11+
provider:
12+
name: aws
13+
runtime: python3.8
14+
lambdaHashingVersion: '20201221'
15+
apiGateway:
16+
shouldStartNameWithService: true
17+
18+
functions:
19+
api:
20+
handler: wsgi_handler.handler
21+
events:
22+
- http:
23+
path: /
24+
method: ANY
25+
- http:
26+
path: /{proxy+}
27+
method: ANY
28+
29+
plugins:
30+
- serverless-wsgi
31+
- serverless-python-requirements

0 commit comments

Comments
 (0)