Follow these steps to deploy this FastAPI app on an AWS EC2 instance using Docker & GitHub Actions.
- Launch an Ubuntu 22.04 EC2 instance.
- Open inbound ports 8080 (API) and 5432 (PostgreSQL) in Security Groups.
- SSH into the instance:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
sudo apt update && sudo apt install -y docker.io docker-compose
sudo systemctl start docker
sudo systemctl enable dockergit clone
cd Orders-API- Create a
.envfile in the root directory:touch .env
- Add the following environment variables to the
.envfile:DB_URL=postgresql://your_username:your_password@your_ec2_publicIP/trades
docker-compose up -d --buildThis project uses GitHub Actions for auto-deployment to EC2.
Go to GitHub Repo → Settings → Secrets → Actions and add the following:
| Secret Name | Value (Example) |
|---|---|
| EC2_HOST | your-ec2-public-ip |
| EC2_USER | ubuntu |
| EC2_SSH_KEY | Your private SSH key (cat ~/.ssh/id_rsa) |
| DOCKERHUB_USERNAME | Your Docker Hub username |
| DOCKERHUB_PASSWORD | Your Docker Hub password or access token |
- Local:
http://localhost:8000 - Deployed:
http://your-ec2-ip:8080
After running the API, visit:
Swagger UI: http://localhost:8080/docs Redoc: http://localhost:8080/redoc
- Description: Fetch all trade orders
- Response:
[ {"id": 1, "name": "AAPL", "quantity": 10, "price": 150.0}, {"id": 2, "name": "TSLA", "quantity": 5, "price": 700.0} ]
- Description: Create a new trade order
- Request:
{"name": "AAPL", "quantity": 10, "price": 150.0} - Response:
{"id": 1, "name": "AAPL", "quantity": 10, "price": 150.0}