0% found this document useful (0 votes)
7 views5 pages

Fastapi Deployment Guide

This document provides a comprehensive guide for deploying a FastAPI application with PostgreSQL using Docker on AWS. It outlines the application architecture, project structure, necessary files (like Dockerfile and docker-compose.yml), and steps for launching the server, installing Docker, and setting up Nginx with HTTPS. Additionally, it includes instructions for implementing a CI/CD pipeline for automatic deployment via GitHub Actions.

Uploaded by

Tuhin Barman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Fastapi Deployment Guide

This document provides a comprehensive guide for deploying a FastAPI application with PostgreSQL using Docker on AWS. It outlines the application architecture, project structure, necessary files (like Dockerfile and docker-compose.yml), and steps for launching the server, installing Docker, and setting up Nginx with HTTPS. Additionally, it includes instructions for implementing a CI/CD pipeline for automatic deployment via GitHub Actions.

Uploaded by

Tuhin Barman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

FastAPI Deployment Guide (Docker + AWS)

Below is a real production workflow to deploy a FastAPI + PostgreSQL app using


Docker on AWS. This is very close to how many startups deploy backend services 🚀.

1. Application Architecture (Production Setup)


Internet

Domain ([Link])

Nginx (Reverse Proxy)

Docker Container

FastAPI App (Uvicorn/Gunicorn)

PostgreSQL Database

2. Project Structure

Example folder structure:

myapp/

├── app/
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]

├── [Link]
├── Dockerfile
├── [Link]

3. Example FastAPI App

[Link]

from fastapi import FastAPI

app = FastAPI()

@[Link]("/")
def home():
return {"message": "App deployed successfully"}

4. [Link]
fastapi
uvicorn
psycopg2-binary
sqlalchemy

5. Dockerfile
FROM python:3.11

WORKDIR /app

COPY . .

RUN pip install -r [Link]

CMD ["uvicorn","[Link]:app","--host","[Link]","--port","8000"]

6. [Link]

This runs FastAPI + PostgreSQL together.

version: "3"

services:

backend:
build: .
ports:
- "8000:8000"
depends_on:
- db

db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
ports:
- "5432:5432"

Run locally:
docker-compose up --build

Test:

[Link]

7. Launch Server on AWS

Create server using Amazon EC2.

Choose:

 Ubuntu
 [Link] (free tier)

SSH into server:

ssh ubuntu@your-server-ip

8. Install Docker on Server


sudo apt update
sudo apt install [Link] -y
sudo apt install docker-compose -y

Start docker:

sudo systemctl start docker

9. Deploy App

Clone your project:

git clone [Link]


cd myapp

Run containers:

sudo docker-compose up -d --build

Now your app runs at:


[Link]

10. Setup Nginx (Production Reverse Proxy)

Install nginx:

sudo apt install nginx

Edit config:

sudo nano /etc/nginx/sites-available/myapp

Example config:

server {
listen 80;

location / {
proxy_pass [Link]
}
}

Enable config:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled


sudo systemctl restart nginx

Now access app using:

[Link]

11. Add HTTPS (SSL)

Install certbot:

sudo apt install certbot python3-certbot-nginx

Run:

sudo certbot --nginx

Your site becomes:

[Link]
12. Real CI/CD (Automatic Deployment)

When you push code to GitHub:

Developer → GitHub → GitHub Actions → AWS Server → Docker Deploy

Example .github/workflows/[Link]

name: Deploy

on:
push:
branches: [ main ]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Deploy to server
run: ssh ubuntu@server-ip "cd myapp && git pull && docker-
compose up -d --build"

Final Production Stack (Common in Startups)


React (Frontend)

Nginx

FastAPI (Docker)

PostgreSQL

AWS EC2

You might also like