Skip to content

Commit

Permalink
Adding blog post to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Laffargue committed Nov 2, 2023
1 parent 43ba250 commit 01ce3db
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 40 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/article.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Publish article

on:
pull_request:
push:
branches:
- main

jobs:
publish-articles-action:
name: Publish article
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4

- name: Carbon markdown
id: carbon-markdown
uses: Cox65/carbon-markdown-action@main
with:
carbonConfigFile: article/carbon-config.json
defaultCarbonPreset: default

- name: Auto-commit carbon images
id: auto-commit-images
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 🤖 Commiting carbon-now changes

- name: Publishing article
id: publishing-article
uses: Cox65/dev-articles-publisher@main
with:
searchPatterns: "article/README.md"
hashnodeApiKey: ${{ secrets.HASHNODE_API_KEY }}
mediumApiKey: ${{ secrets.MEDIUM_API_KEY }}
devToApiKey: ${{ secrets.DEVTO_API_KEY }}

- name: Auto-commit
id: auto-commit
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: 🤖 Commiting publications metadata
112 changes: 112 additions & 0 deletions article/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: ⚡ Serverless REST API on AWS with FastAPI ⚡
tags:
- Python
- AWS
canonicalUrl: https://medium.com/aws-tip/serverless-rest-api-on-aws-with-fastapi-bd9de11f925a
coverImage: >-
https://github.com/DevWaveX/fastapi-aws-starter-kit/raw/main/articles/fastapi-serverless-aws/article/cover.png
publications:
- platform: devTo
published: false
- platform: hashnode
publicationId: 62019a434efba97010a97bb9
---

# ⚡ Serverless REST API on AWS with FastAPI ⚡

Hi there! 👊

In this article, I will explain you how to deploy, in minutes, **FastAPI** on **AWS** using **Serverless**.

As usual in all my articles, you will find at the end a link to a working example on my Github.

## Pre-requisites

I assume you have at least **Python 3.7+** and **npm** installed.

### Install and configure AWS CLI

You need to install the **AWS CLI** and configure your credentials. You can follow this [link](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) for instructions.

### Install Serverless Framework

Then you will have to install the **Serverless** framework via npm. See instructions [here](https://www.serverless.com/framework/docs/getting-started#via-npm).

## FastAPI Application

### Build the application

First of all, we will build our FastAPI application. Add **fastapi** dependency in your requirements.txt and install it.
Then you can create your FastAPI application:
[{"filename": "../fastapi_aws_starter_kit/fastapi_app.py"}]: 🎨

## Deploy FastAPI on AWS

To deploy our API in **AWS**, we will leverage **API Gateway** and **Lambda** services and use **Serverless** framework capabilities to easily create all resources we need.

### Lambda handler with Mangum

In order to build the handler for our Lambda which will be called by our API Gateway, we will use [**Mangum**](https://github.com/jordaneremieff/mangum) which is an ASGI adapter for API Gateway and Lambda (perfect for our FastAPI application 🍾)

Add **mangum** to your dependencies and build the handler as follow:
[{"filename": "../fastapi_aws_starter_kit/handler.py"}]: 🎨

### Configure serverless.yaml

Serverless configuration is pretty easy, here is what I did as a simple example:
[{"filename": "../serverless.yaml"}]: 🎨

As you can see I’m using 2 plugins:

- **serverless-python-requirements:** It will bundle your python dependencies specified in your requirements.txt

- **serverless-offline:** It will allow to run our infrastructure locally by running **sls offline** command.

### Deploy your API

To deploy your API on AWS, you just need to perform:

```sh
sls deploy
```

## Run your API in local

If you want to run your API in local, you have two options.
Either you can run an [**uvicorn**](https://www.uvicorn.org/) web server like this:
[{"filename": "../fastapi_aws_starter_kit/web_server.py"}]: 🎨

Either, and this what I recommend as we are using Serverless, you can use **serverless-offline** plugin I just mentionned before.

```sh
➜ sls offline
Running "serverless" from node_modules
Using local credentials. Add provider credentials via dashboard: https://app.serverless.com//providers

Starting Offline at stage dev (eu-west-1)

Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
* app: fastapi-aws-starter-kit-dev-app

┌───────────────────────────────────────────────────────────────────────┐
│ │
│ ANY | http://localhost:3000/dev/{proxy*} │
│ POST | http://localhost:3000/2015-03-31/functions/app/invocations │
│ │
└───────────────────────────────────────────────────────────────────────┘

Server ready: http://localhost:3000
```

And that’s it ! 🚀

## FastAPI AWS Starter Kit

Here the the repository related to this article:
[**GitHub - Cox65/fastapi-aws-starter-kit: Starter kit to deploy FastAPI as an AWS serverless…**](https://github.com/Cox65/fastapi-aws-starter-kit)

I hope you enjoyed reading this article and that it will help you in your next projects ! 🤘

If you liked it, don’t forget to clap, share and subscribe 😉
8 changes: 8 additions & 0 deletions article/carbon-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"default": {
"backgroundColor": "rgba(0, 0, 0, 0)",
"theme": "night-owl",
"windowTheme": "none",
"fontFamily": "Hack"
}
}
Binary file added article/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 0 additions & 40 deletions fastapi_aws_starter_kit/.github/workflows/main.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions fastapi_aws_starter_kit/web_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import uvicorn

if __name__ == "__main__":
uvicorn.run(
"fastapi_app:app",
host="127.0.0.1",
port=5000,
reload=True,
)

0 comments on commit 01ce3db

Please sign in to comment.