-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AWS Python FastAPI Basic Example Implementation (#731)
- Loading branch information
1 parent
fc433a7
commit 2c57d9a
Showing
8 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Environments | ||
.env | ||
.venv | ||
venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Serverless directories | ||
.serverless | ||
|
||
# Others | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<!-- | ||
title: 'Serverless Framework Python FastAPI on AWS' | ||
description: 'This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework.' | ||
layout: Doc | ||
framework: v3 | ||
platform: AWS | ||
language: Python | ||
priority: 2 | ||
authorLink: 'https://github.com/FernandoCelmer' | ||
authorName: 'FernandoCelmer' | ||
authorAvatar: 'https://avatars1.githubusercontent.com/u/6262214?v=4' | ||
--> | ||
|
||
# Serverless Framework Python FastAPI on AWS | ||
|
||
<img src="https://img.shields.io/badge/Python-3776AB?style=for-the-badge&logo=python&logoColor=white"> | ||
<img src="https://img.shields.io/badge/Amazon_AWS-232F3E?style=for-the-badge&logo=amazon-aws&logoColor=white"> | ||
|
||
This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework. | ||
|
||
# Usage | ||
|
||
## Prerequisites FastAPI | ||
|
||
- Python >= 3.10 | ||
- ![PyPI](https://img.shields.io/pypi/v/fastapi?color=blue&label=fastapi&style=flat-square) | ||
- ![PyPI](https://img.shields.io/pypi/v/uvicorn?color=blue&label=uvicorn&style=flat-square) | ||
- ![PyPI](https://img.shields.io/pypi/v/mangum?color=blue&label=mangum&style=flat-square) | ||
|
||
## Prerequisites Serverless Framework | ||
|
||
- Nodejs >= 18.12.1 | ||
- ![npm](https://img.shields.io/npm/v/serverless-python-requirements?label=serverless-python-requirements&style=flat-square) | ||
|
||
|
||
# Deployment | ||
|
||
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc. | ||
|
||
In order to deploy with dashboard, you need to first login with: | ||
|
||
``` | ||
serverless login | ||
``` | ||
|
||
install dependencies with: | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
and | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
and then perform deployment with: | ||
|
||
``` | ||
serverless deploy | ||
``` | ||
|
||
After running deploy, you should see output similar to: | ||
|
||
```bash | ||
Running "serverless" from node_modules | ||
|
||
Deploying aws-python-fastapi to stage dev (us-east-1) | ||
|
||
✔ Service deployed to stack aws-python-fastapi-dev (140s) | ||
|
||
endpoint: ANY - https://xxxxxxxx.execute-api.us-east-1.amazonaws.com | ||
functions: | ||
app: aws-python-fastapi-dev-app (42 MB) | ||
``` | ||
|
||
|
||
## Invocation | ||
|
||
After successful deployment, you can call the created application via HTTP: | ||
|
||
```bash | ||
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/ | ||
``` | ||
|
||
Which should result in the following response: | ||
|
||
``` | ||
AWS Python FastAPI | ||
``` | ||
|
||
Calling the `/status` path with: | ||
|
||
```bash | ||
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/status | ||
``` | ||
|
||
Should result in the following response: | ||
|
||
```bash | ||
its alive | ||
``` | ||
|
||
If you try to invoke a path or method that does not have a configured handler, e.g. with: | ||
|
||
```bash | ||
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/whatever | ||
``` | ||
|
||
You should receive the following response: | ||
|
||
```bash | ||
{"detail":"Not Found"} | ||
``` | ||
|
||
## Local development | ||
|
||
To run the `aws-python-fastapi` project on your local machine, just follow the following commands. | ||
|
||
- Create a new Python virtual environment | ||
```bash | ||
python3.9 -m venv ./venv | ||
``` | ||
|
||
- Activate the virtual environment | ||
```bash | ||
source venv/bin/activate | ||
``` | ||
|
||
- Install requirements with PIP | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
- Run the application | ||
``` | ||
uvicorn app:app --host 0.0.0.0 --reload | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from fastapi import FastAPI | ||
from fastapi.responses import Response | ||
from mangum import Mangum | ||
|
||
|
||
app = FastAPI( | ||
title="API", | ||
version="0.0.1", | ||
description="AWS Python FastAPI" | ||
) | ||
|
||
@app.get("/") | ||
def hello_from_root(): | ||
return Response(content="AWS Python FastAPI") | ||
|
||
|
||
@app.get("/status") | ||
def my_status(): | ||
return Response(content="it's alive") | ||
|
||
|
||
handler = Mangum(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "aws-python-fastapi", | ||
"version": "1.0.0", | ||
"description": "Example API service with Python using FastAPI and Serverless Framework on AWS", | ||
"author": "FernandoCelmer", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"serverless-python-requirements": "^6.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fastapi==0.88.0 | ||
uvicorn==0.20.0 | ||
mangum==0.17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
service: aws-python-fastapi | ||
|
||
frameworkVersion: '3' | ||
|
||
package: | ||
individually: true | ||
|
||
provider: | ||
name: aws | ||
runtime: python3.9 | ||
|
||
functions: | ||
app: | ||
handler: app.handler | ||
events: | ||
- httpApi: '*' | ||
|
||
plugins: | ||
- serverless-python-requirements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters