Skip to content

Commit 2c57d9a

Browse files
AWS Python FastAPI Basic Example Implementation (serverless#731)
1 parent fc433a7 commit 2c57d9a

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ serverless install -u https://github.com/serverless/examples/tree/v3/folder-name
116116
| [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 |
117117
| [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 |
118118
| [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 |
119+
| [Aws Python FastApi](https://github.com/serverless/examples/tree/master/aws-python-fastapi) <br/> Example API service with Python using FastAPI and Serverless Framework on AWS. | python |
119120
| [Aws Python Flask Dynamodb Api](https://github.com/serverless/examples/tree/master/aws-python-flask-dynamodb-api) <br/> Example of a Python Flask API service backed by DynamoDB with traditional Serverless Framework | python |
120121
| [Aws Http With Dynamodb](https://github.com/serverless/examples/tree/master/aws-python-http-api-with-dynamodb) <br/> Serverless HTTP API | python |
121122
| [Aws Http With Pynamodb](https://github.com/serverless/examples/tree/master/aws-python-http-api-with-pynamodb) <br/> Serverless CRUD service exposing an HTTP API | python |

aws-python-fastapi/.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
10+
__pypackages__/
11+
12+
# Environments
13+
.env
14+
.venv
15+
venv
16+
env/
17+
venv/
18+
ENV/
19+
env.bak/
20+
venv.bak/
21+
22+
# Serverless directories
23+
.serverless
24+
25+
# Others
26+
node_modules

aws-python-fastapi/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<!--
2+
title: 'Serverless Framework Python FastAPI on AWS'
3+
description: 'This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework.'
4+
layout: Doc
5+
framework: v3
6+
platform: AWS
7+
language: Python
8+
priority: 2
9+
authorLink: 'https://github.com/FernandoCelmer'
10+
authorName: 'FernandoCelmer'
11+
authorAvatar: 'https://avatars1.githubusercontent.com/u/6262214?v=4'
12+
-->
13+
14+
# Serverless Framework Python FastAPI on AWS
15+
16+
<img src="https://img.shields.io/badge/Python-3776AB?style=for-the-badge&logo=python&logoColor=white">
17+
<img src="https://img.shields.io/badge/Amazon_AWS-232F3E?style=for-the-badge&logo=amazon-aws&logoColor=white">
18+
19+
This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework.
20+
21+
# Usage
22+
23+
## Prerequisites FastAPI
24+
25+
- Python >= 3.10
26+
- ![PyPI](https://img.shields.io/pypi/v/fastapi?color=blue&label=fastapi&style=flat-square)
27+
- ![PyPI](https://img.shields.io/pypi/v/uvicorn?color=blue&label=uvicorn&style=flat-square)
28+
- ![PyPI](https://img.shields.io/pypi/v/mangum?color=blue&label=mangum&style=flat-square)
29+
30+
## Prerequisites Serverless Framework
31+
32+
- Nodejs >= 18.12.1
33+
- ![npm](https://img.shields.io/npm/v/serverless-python-requirements?label=serverless-python-requirements&style=flat-square)
34+
35+
36+
# Deployment
37+
38+
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
39+
40+
In order to deploy with dashboard, you need to first login with:
41+
42+
```
43+
serverless login
44+
```
45+
46+
install dependencies with:
47+
48+
```
49+
npm install
50+
```
51+
52+
and
53+
54+
```
55+
pip install -r requirements.txt
56+
```
57+
58+
and then perform deployment with:
59+
60+
```
61+
serverless deploy
62+
```
63+
64+
After running deploy, you should see output similar to:
65+
66+
```bash
67+
Running "serverless" from node_modules
68+
69+
Deploying aws-python-fastapi to stage dev (us-east-1)
70+
71+
✔ Service deployed to stack aws-python-fastapi-dev (140s)
72+
73+
endpoint: ANY - https://xxxxxxxx.execute-api.us-east-1.amazonaws.com
74+
functions:
75+
app: aws-python-fastapi-dev-app (42 MB)
76+
```
77+
78+
79+
## Invocation
80+
81+
After successful deployment, you can call the created application via HTTP:
82+
83+
```bash
84+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
85+
```
86+
87+
Which should result in the following response:
88+
89+
```
90+
AWS Python FastAPI
91+
```
92+
93+
Calling the `/status` path with:
94+
95+
```bash
96+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/status
97+
```
98+
99+
Should result in the following response:
100+
101+
```bash
102+
its alive
103+
```
104+
105+
If you try to invoke a path or method that does not have a configured handler, e.g. with:
106+
107+
```bash
108+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/whatever
109+
```
110+
111+
You should receive the following response:
112+
113+
```bash
114+
{"detail":"Not Found"}
115+
```
116+
117+
## Local development
118+
119+
To run the `aws-python-fastapi` project on your local machine, just follow the following commands.
120+
121+
- Create a new Python virtual environment
122+
```bash
123+
python3.9 -m venv ./venv
124+
```
125+
126+
- Activate the virtual environment
127+
```bash
128+
source venv/bin/activate
129+
```
130+
131+
- Install requirements with PIP
132+
```bash
133+
pip install -r requirements.txt
134+
```
135+
136+
- Run the application
137+
```
138+
uvicorn app:app --host 0.0.0.0 --reload
139+
```

aws-python-fastapi/app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from fastapi import FastAPI
2+
from fastapi.responses import Response
3+
from mangum import Mangum
4+
5+
6+
app = FastAPI(
7+
title="API",
8+
version="0.0.1",
9+
description="AWS Python FastAPI"
10+
)
11+
12+
@app.get("/")
13+
def hello_from_root():
14+
return Response(content="AWS Python FastAPI")
15+
16+
17+
@app.get("/status")
18+
def my_status():
19+
return Response(content="it's alive")
20+
21+
22+
handler = Mangum(app)

aws-python-fastapi/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-fastapi",
3+
"version": "1.0.0",
4+
"description": "Example API service with Python using FastAPI and Serverless Framework on AWS",
5+
"author": "FernandoCelmer",
6+
"license": "MIT",
7+
"devDependencies": {
8+
"serverless-python-requirements": "^6.0.0"
9+
}
10+
}

aws-python-fastapi/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fastapi==0.88.0
2+
uvicorn==0.20.0
3+
mangum==0.17.0

aws-python-fastapi/serverless.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service: aws-python-fastapi
2+
3+
frameworkVersion: '3'
4+
5+
package:
6+
individually: true
7+
8+
provider:
9+
name: aws
10+
runtime: python3.9
11+
12+
functions:
13+
app:
14+
handler: app.handler
15+
events:
16+
- httpApi: '*'
17+
18+
plugins:
19+
- serverless-python-requirements

examples.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,5 +2550,18 @@
25502550
"authorName": "Bref",
25512551
"authorAvatar": "https://avatars.githubusercontent.com/u/45861341?v=4",
25522552
"community": true
2553+
},
2554+
{
2555+
"title": "Serverless Framework Python FastAPI on AWS",
2556+
"name": "aws-python-fastapi",
2557+
"description": "This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework.",
2558+
"githubUrl": "https://github.com/serverless/examples/tree/v3/aws-python-fastapi",
2559+
"framework": "v3",
2560+
"language": "python",
2561+
"platform": "aws",
2562+
"authorLink": "https://github.com/FernandoCelmer",
2563+
"authorName": "FernandoCelmer",
2564+
"authorAvatar": "https://avatars1.githubusercontent.com/u/6262214?v=4",
2565+
"community": true
25532566
}
25542567
]

0 commit comments

Comments
 (0)