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