Deploy FastAPI to AWS Elastic Beanstalk (Zero to Hero)

This guide will go over how to deploy a FastAPI app, add a `postgres` database, and attach a SSL certificate (assuming you have purchased a custom domain).

Prerequisites

This tutorial will be using pipx to install the Elastic Beanstalk CLI. Follow the instructions here to install AWS’s CLI if you do not already have that installed. Then configure it following this guide.

FastAPI Project

Assuming that you are using poetry to manage your dependencies, run the following commands in your terminal:

poetry new deployment-example
cd deployment-example
poetry add fastapi "uvicorn[standard]" tortoise-orm pydantic

Our folder structure will look something like this:

.
????????? README.rst
????????? deployment_example
???   ????????? __init__.py
????????? poetry.lock
????????? pyproject.toml
????????? tests
    ????????? __init__.py
    ????????? test_deployment_example.py

Note that we are using tortoise-orm as our ORM here, but feel free to use anything else (e.g., sqlmodelsqlalchemy, etc…). Notice that we installed the standard version of uvicorn that will help us later on during deployment. Create a main.py file inside of deployment_example to start coding our API:

Learn More

Tags: AWS FastAPI