This project is a comprehensive CRUD microservice built with FastAPI. It utilizes PostgreSQL for structured data, MongoDB for unstructured data, and is containerized with Docker for easy setup and deployment. It also integrates with AWS S3 for file storage and includes a CI/CD pipeline using GitHub Actions for automated testing and deployment.
- Features
- Technologies Used
- Project Structure
- Architecture
- Getting Started
- API Endpoints
- Database Migrations
- Running Tests
- Deployment
- Contributing
- Author
- Acknowledgements
- Modern Python Framework: Built with FastAPI for high performance and automatic interactive documentation.
- Dual Database Architecture:
- PostgreSQL: For core relational data like users, products, and orders.
- MongoDB: For unstructured data like OTPs and token blacklists.
- Authentication & Authorization: Robust JWT-based authentication with access and refresh tokens, password hashing, and role-based access control (superusers).
- Containerized Environment: Docker and Docker Compose for consistent development, testing, and production environments.
- Cloud Storage Integration: Seamlessly integrated with AWS S3 for file uploads and management.
- Database Migrations: Uses Alembic for managing PostgreSQL schema migrations.
- Asynchronous Operations: Fully asynchronous from the API layer down to the database for non-blocking I/O.
- Comprehensive Testing: Unit and integration tests using Pytest to ensure code quality and reliability.
- CI/CD Pipeline: Automated testing and deployment pipeline configured with GitHub Actions.
- Backend: FastAPI, Python 3.13
- Databases: PostgreSQL, MongoDB
- Containerization: Docker, Docker Compose
- Cloud Services: AWS S3, AWS EC2, AWS RDS
- Authentication: JWT, Passlib, python-jose
- ORM/ODM: SQLAlchemy (asyncio), Motor
- Migrations: Alembic
- Testing: Pytest, Pytest-asyncio, HTTPX
- CI/CD: GitHub Actions
fastapi-cloud-microservice/
│── app/ # Main application package
│ ├── api/v1/ # API route definitions
│ │ ├── routes_auth.py # Authentication routes
│ │ ├── routes_users.py # User CRUD
│ │ ├── routes_orders.py # Orders CRUD
│ │ ├── routes_products.py # Products CRUD
│ │ ├── routes_s3.py # File upload/download to S3
│ │ └── routes_health.py # Health check
│ ├── core/ # Core business logic & utilities
│ │ ├── auth.py # JWT, OAuth2 handling
│ │ ├── config.py # App settings (from .env)
│ │ ├── email.py # Email service (SMTP)
│ │ ├── s3.py # AWS S3 integration
│ │ └── security.py # Password hashing, security utils
│ ├── db/ # Database configs
│ │ ├── models.py # SQLAlchemy models
│ │ ├── pg.py # PostgreSQL session
│ │ └── mongo.py # MongoDB connection
│ ├── repos/ # Repository layer (DB operations)
│ │ ├── user_repo.py # User persistence
│ │ ├── order_repo.py # Orders persistence
│ │ └── product_repo.py # Products persistence
│ ├── schemas/ # Pydantic models (validation/serialization)
│ └── main.py # FastAPI entrypoint
│── alembic/ # DB migrations (PostgreSQL)
│── postgres-init/ # Init scripts for DB
│── scripts/ # Helper scripts (migrations, seeding)
│── tests/ # Unit & API tests (pytest)
│── docker-compose.yml # Local development
│── docker-compose.prod.yml # Production compose (AWS, RDS, S3)
│── Dockerfile # Service container
│── entrypoint.sh # Entrypoint for Docker
│── alembic.ini # Alembic configuration
│── pyproject.toml # Project dependencies and metadata
│── .github/workflows/ # CI/CD workflows
│── .env.example # Example environment config
│── makefile # Useful commands
└── README.md # Project documentation
flowchart TD
Client[Client / Frontend] --> LB[Load Balancer / Nginx]
LB --> API[FastAPI Service - Docker on EC2]
API --> PG[(PostgreSQL - AWS RDS)]
API --> MG[(MongoDB - Atlas)]
API --> S3[(AWS S3 - File Storage)]
Dev[GitHub Actions CI/CD] --> Registry[Docker Registry - ECR/DockerHub]
Registry --> EC2[(AWS EC2 Instance)]
EC2 --> API
- Docker and Docker Compose
- An AWS account with an S3 bucket and IAM credentials
- Python 3.13+ (for local development without Docker)
-
Clone the repository:
git clone <repository-url> cd fastapi-cloud-microservice
-
Configure Environment Variables: Create a
.envfile from the example:cp .env.example .env
Update the
.envfile with your specific configurations for PostgreSQL, MongoDB, AWS, and JWT secrets. -
Build and Run the Containers: Use the provided
makefile:make up
-
Access the Application:
- API:
http://localhost:8000 - Interactive Docs (Swagger UI):
http://localhost:8000/docs - Alternative Docs (ReDoc):
http://localhost:8000/redoc
- API:
-
Create a virtual environment:
python -m venv .venv source .venv/bin/activate -
Install dependencies:
pip install -e ".[dev]" -
Run the application:
uvicorn app.main:app --reload
| Command | Description |
|---|---|
make up |
Start services (dev) |
make down |
Stop services |
make migrate |
Run Alembic migrations |
make revision |
Create new migration |
make seed |
Seed DB with sample data |
make test |
Run tests |
The API is versioned under /api/v1/. Here is a summary of the available endpoints:
POST /register: Register a new user.POST /login: Authenticate and receive JWT tokens.POST /logout: Blacklist the current user's token.POST /refresh: Obtain a new access token using a refresh token.POST /password-reset/request: Request a password reset OTP.POST /password-reset/verify: Verify OTP and set a new password.
POST /: (Admin) Create a new user.GET /: (Admin) List all users.GET /me: Get the current authenticated user's details.PUT /me: Update the current authenticated user's details.DELETE /{user_id}: (Admin) Delete a user.
POST /: (Admin) Create a new product.GET /: List all available products.GET /{product_id}: Get details of a specific product.PUT /{product_id}: (Owner/Admin) Update a product.DELETE /{product_id}: (Owner/Admin) Delete a product.POST /{product_id}/image: (Owner/Admin) Upload an image for a product.GET /{product_id}/image-url: Get a presigned URL for a product's image.
POST /: Create a new order.GET /: List the current user's orders.GET /{order_id}: (Owner/Admin) Get a specific order.POST /{order_id}/cancel: (Owner/Admin) Cancel an order.GET /admin/all: (Admin) List all orders from all users.
POST /upload: Upload a file to AWS S3.GET /file/{file_key}: Get a presigned URL for a file.DELETE /file/{file_key}: Delete a file from S3.
GET /live: Liveness probe endpoint.GET /ready: Readiness probe endpoint.
Database schema migrations for PostgreSQL are handled by Alembic.
-
Generate a new migration:
make revision
This will autogenerate a new revision file in
alembic/versions/. -
Apply migrations: The
entrypoint.shscript automatically applies migrations on startup. To apply them manually:make migrate
The project has a comprehensive test suite using Pytest.
- Run all tests:
This command executes all tests inside a dedicated Docker container, ensuring a clean and isolated testing environment.
make test
The provided docker-compose.prod.yml file is configured for a production-like deployment. It uses a production-specific .env.prod file and exposes the API on port 80.
A sample production environment might involve:
- Running the application on an AWS EC2 instance.
- Using AWS RDS for the PostgreSQL database.
- Using MongoDB Atlas for the MongoDB cluster.
- Configuring a reverse proxy like Nginx or an AWS Application Load Balancer.
A GitHub Actions workflow can be set up to automate the following:
- On push to
mainor pull request:- Lint the code.
- Run the test suite against a test database.
- On merge to
main:- Build and push the Docker image to a registry (e.g., Docker Hub, AWS ECR).
- Deploy the new image to the EC2 instance.
Contributions are welcome! Please feel free to submit a pull request or open an issue.
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
Ayush Kaneriya - ayushkaneriya05@gmail.com
Project Link: https://github.com/ayushkaneriya05/fastapi-cloud-microservice
Deployed URL: fastapi-cloud-microservice
This project would not have been possible without the amazing open-source ecosystem and cloud services:
-
Frameworks & Libraries
-
Cloud & Infrastructure
- AWS EC2 – Hosting
- AWS S3 – Object storage
- AWS RDS – Managed PostgreSQL
- MongoDB Atlas – Managed MongoDB
- GitHub Actions – CI/CD automation
- Docker – Containerization