Skip to content

severlansdev/cloud-native-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

☁️ Cloud-Native Production-Ready API

A production-grade FastAPI application with DevSecOps, IaC, and full observability

Quick Start β€’ Architecture β€’ Tech Stack β€’ Infrastructure β€’ CI/CD β€’ Monitoring

Python FastAPI Docker Terraform GitHub Actions Trivy Monitoring


πŸ“‹ Overview

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

πŸš€ Quick Start

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)

Local Development (without Docker)

# 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

πŸ›οΈ Architecture

Cloud Infrastructure (AWS)

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚           AWS Cloud (VPC)             β”‚
                    β”‚                                      β”‚
   Internet ──────▢│  ALB (:80) ──▢ ECS Fargate Cluster  β”‚
                    β”‚                 β”œβ”€ Task #1 (AZ-1)   β”‚
                    β”‚                 └─ Task #2 (AZ-2)   β”‚
                    β”‚                                      β”‚
                    β”‚  ECR (Images)   CloudWatch (Logs)    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Local Development Stack

   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


πŸ› οΈ Tech Stack

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

πŸ—οΈ Infrastructure

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

Key Infrastructure Features

  • 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

Test with LocalStack (No AWS Account Needed)

# 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"

πŸ”„ CI/CD Pipeline

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)

πŸ“Š Monitoring

Pre-configured Grafana Dashboard

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

Endpoints

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)

πŸ“ Project Structure

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

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Run tests (make test)
  4. Run linter (make lint)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with ❀️ by Brayan PD β€” DevOps / SRE Engineer

About

Production-ready FastAPI with DevSecOps, Terraform IaC, and full observability

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors