A production-grade FastAPI application with DevSecOps, IaC, and full observability
Quick Start β’ Architecture β’ Tech Stack β’ Infrastructure β’ CI/CD β’ Monitoring
This project demonstrates production-ready cloud-native practices for deploying and operating a REST API. It's not just a "Hello World" β it's the complete DevOps/SRE toolkit you'd use in a real production environment:
| Feature | What it demonstrates |
|---|---|
| π³ Multi-stage Distroless Docker | Secure, minimal images (~80MB vs ~900MB) |
| ποΈ Terraform IaC | Full AWS infrastructure: VPC, ECR, ECS Fargate, ALB |
| π CI/CD Pipeline | Automated lint β test β security scan β build β deploy |
| π‘οΈ DevSecOps | Trivy CVE scanning on every build |
| π Observability | Prometheus metrics + Grafana dashboards |
| π©Ί Health Probes | Kubernetes/ECS-compatible liveness & readiness checks |
| π Structured Logging | JSON logs ready for ELK/CloudWatch/Datadog |
| π§ͺ Testing | pytest with 80%+ coverage gate |
Get the full stack running in 3 commands:
# 1. Clone the repository
git clone https://github.com/severlansdev/cloud-native-api.git
cd cloud-native-api
# 2. Start the full stack (API + Prometheus + Grafana + LocalStack)
make up
# 3. Open the API docs
# π API: http://localhost:8000
# π Swagger: http://localhost:8000/docs
# β€οΈ Health: http://localhost:8000/health
# π Metrics: http://localhost:8000/metrics
# π₯ Prometheus: http://localhost:9090
# π Grafana: http://localhost:3000 (admin/admin)# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Linux/Mac
.venv\Scripts\activate # Windows
# Install dependencies
make install
# Run with hot-reload
make dev
# Run tests
make test
# Lint
make lint ββββββββββββββββββββββββββββββββββββββββ
β AWS Cloud (VPC) β
β β
Internet βββββββΆβ ALB (:80) βββΆ ECS Fargate Cluster β
β ββ Task #1 (AZ-1) β
β ββ Task #2 (AZ-2) β
β β
β ECR (Images) CloudWatch (Logs) β
ββββββββββββββββββββββββββββββββββββββββ
docker compose up
β
βββ FastAPI API (:8000) β Your application
βββ Prometheus (:9090) β Metrics collection
βββ Grafana (:3000) β Dashboards
βββ LocalStack (:4566) β AWS emulation
π For detailed architecture diagrams, see
docs/architecture.md
| Layer | Technology | Purpose |
|---|---|---|
| Application | Python 3.12 + FastAPI | High-performance async REST API |
| Configuration | Pydantic Settings | Type-safe, 12-Factor App config |
| Logging | structlog (JSON) | Machine-parseable structured logs |
| Metrics | prometheus-fastapi-instrumentator | Auto-instrumented Prometheus metrics |
| Container | Docker (Distroless) | Minimal, secure runtime image |
| Orchestration | Docker Compose | Local multi-service development |
| IaC | Terraform | AWS infrastructure provisioning |
| CI/CD | GitHub Actions | Automated pipeline with 5 stages |
| Security | Trivy | Container vulnerability scanning |
| Monitoring | Prometheus + Grafana | Metrics collection & visualization |
| AWS Mock | LocalStack | Free local AWS emulation |
| Testing | pytest + httpx | Async API testing with coverage |
| Linting | ruff | Fast Python linter & formatter |
All infrastructure is defined as code using Terraform and designed for AWS:
terraform/
βββ main.tf # Provider config (AWS + LocalStack toggle)
βββ variables.tf # Parameterized variables with validation
βββ vpc.tf # VPC, subnets (2 AZs), IGW, security groups
βββ ecr.tf # Container registry with lifecycle policies
βββ ecs.tf # Fargate cluster, task definition, service
βββ alb.tf # Application Load Balancer + health checks
βββ outputs.tf # Resource IDs and API URL
βββ terraform.tfvars.example
- Multi-AZ deployment for high availability
- Security groups with least-privilege access (ALB β ECS only)
- Immutable image tags in ECR for deployment safety
- Container Insights enabled for ECS monitoring
- CloudWatch Logs with 30-day retention
- LocalStack toggle for free local testing
# Start LocalStack
docker compose up localstack -d
# Initialize resources
bash scripts/localstack-init.sh
# Run Terraform against LocalStack
cd terraform
terraform init
terraform apply -var="use_localstack=true"The GitHub Actions pipeline runs on every push and PR:
βββββββββββ ββββββββββββ βββββββββββββββββ ββββββββββββββββ ββββββββββββ
β Lint ββββββΆβ Test ββββββΆβ Security Scan ββββββΆβ Docker Build ββββββΆβ Push ECR β
β (ruff) β β (pytest) β β (Trivy) β β (Distroless) β β (AWS) β
βββββββββββ β cov β₯80% β β CRITICAL/HIGH β ββββββββββββββββ ββββββββββββ
ββββββββββββ βββββββββββββββββ
- Lint: Code quality enforcement with
ruff - Test: Unit tests with 80% minimum coverage
- Security: Trivy scans for CRITICAL/HIGH CVEs, results uploaded to GitHub Security tab
- Build: Multi-stage Docker build with OCI labels
- Push: Conditional push to AWS ECR (only on
main, only if AWS secrets are configured)
The project includes a production-ready Grafana dashboard with:
| Panel | Metric |
|---|---|
| π’ Request Rate | Requests per second by method/status |
| β±οΈ Latency | P50, P95, P99 response times |
| π΄ Error Rate | 5xx error percentage with thresholds |
| π Total Requests | Cumulative request counter |
| β¬οΈ Uptime | Process uptime in seconds |
| πΎ Memory | Resident memory usage |
| π Status Codes | Pie chart of HTTP status distribution |
| π‘οΈ Duration Heatmap | Request duration distribution |
| Endpoint | Purpose |
|---|---|
GET /health |
Liveness probe (is the process alive?) |
GET /ready |
Readiness probe (is the service ready?) |
GET /metrics |
Prometheus metrics endpoint |
GET /docs |
Swagger UI (interactive API docs) |
GET /redoc |
ReDoc (alternative API docs) |
cloud-native-api/
βββ app/ # Application source code
β βββ main.py # FastAPI app with health probes
β βββ config.py # 12-Factor config (Pydantic Settings)
β βββ routers/items.py # CRUD REST endpoints
β βββ middleware/logging.py # Structured JSON logging
β βββ tests/ # pytest test suite (80%+ coverage)
βββ terraform/ # AWS Infrastructure as Code
βββ monitoring/ # Prometheus + Grafana configs
β βββ prometheus/ # Scrape configuration
β βββ grafana/ # Dashboards + datasource provisioning
βββ .github/workflows/ci.yml # CI/CD pipeline (5 stages)
βββ Dockerfile # Multi-stage β Distroless
βββ docker-compose.yml # Full observability stack
βββ Makefile # Developer-friendly commands
βββ scripts/ # Utility scripts
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Run tests (
make test) - Run linter (
make lint) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ by Brayan PD β DevOps / SRE Engineer