Skip to content

Commit

Permalink
AWS Python FastAPI Basic Example Implementation (serverless#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
FernandoCelmer committed Dec 21, 2022
1 parent fc433a7 commit 2c57d9a
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ serverless install -u https://github.com/serverless/examples/tree/v3/folder-name
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [Aws Http With Dynamodb](https://github.com/serverless/examples/tree/master/aws-python-http-api-with-dynamodb) <br/> Serverless HTTP API | python |
| [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 |
Expand Down
26 changes: 26 additions & 0 deletions aws-python-fastapi/.gitignore
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
139 changes: 139 additions & 0 deletions aws-python-fastapi/README.md
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
```
22 changes: 22 additions & 0 deletions aws-python-fastapi/app.py
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)
10 changes: 10 additions & 0 deletions aws-python-fastapi/package.json
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"
}
}
3 changes: 3 additions & 0 deletions aws-python-fastapi/requirements.txt
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
19 changes: 19 additions & 0 deletions aws-python-fastapi/serverless.yml
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
13 changes: 13 additions & 0 deletions examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -2550,5 +2550,18 @@
"authorName": "Bref",
"authorAvatar": "https://avatars.githubusercontent.com/u/45861341?v=4",
"community": true
},
{
"title": "Serverless Framework Python FastAPI on AWS",
"name": "aws-python-fastapi",
"description": "This template demonstrates how to develop and deploy a simple API in Python with FastAPI, running on AWS Lambda using the Serverless Framework.",
"githubUrl": "https://github.com/serverless/examples/tree/v3/aws-python-fastapi",
"framework": "v3",
"language": "python",
"platform": "aws",
"authorLink": "https://github.com/FernandoCelmer",
"authorName": "FernandoCelmer",
"authorAvatar": "https://avatars1.githubusercontent.com/u/6262214?v=4",
"community": true
}
]

0 comments on commit 2c57d9a

Please sign in to comment.