Skip to content

A DevOps learning sandbox demonstrating an end-to-end CI/CD pipeline for a FastAPI service, from build and test through containerization and deployment, using Terraform, Ansible, and GitHub Actions.

License

Notifications You must be signed in to change notification settings

lele-sf/fastapi-devops-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI DevOps Pipeline

CI/CD Pipeline Python 3.12 License: MIT

A complete DevOps project featuring a FastAPI application with a live system metrics dashboard, infrastructure provisioning with Terraform, automated configuration with Ansible, and containerization with Docker.

πŸ“‹ Table of Contents

Overview

This project is a DevOps learning sandbox that demonstrates modern infrastructure and deployment practices. The application itself, a simple system metrics API, serves as a minimal but functional payload for the pipeline.

Practice Tool Description
Infrastructure as Code Terraform Automated AWS provisioning
Configuration Management Ansible Zero-touch server setup
Containerization Docker Multi-stage builds for optimized images
CI/CD GitHub Actions Lint, test, build, and deploy pipeline
Monitoring Gradio + Plotly Real-time metrics dashboard

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                            GitHub Actions                                β”‚
β”‚                    (Lint β†’ Test β†’ Build β†’ Deploy)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Terraform     │────▢│     Ansible      │────▢│   Application    β”‚
β”‚  (AWS EC2, SG)   β”‚     β”‚ (Docker, Config) β”‚     β”‚    (FastAPI)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Prerequisites

Quick Start

# Clone the repository
git clone git@github.com:lele-sf/fastapi-devops-pipeline.git
cd fastapi-devops-pipeline

# Install dependencies
uv sync

# Run the application
uv run uvicorn src.app.main:app --reload

Open your browser:

Local Development

Installation

# Install all dependencies (including dev)
uv sync

# Install pre-commit hooks
uv run pre-commit install

# Activate the virtual environment (optional, uv run handles this)
source .venv/bin/activate  # Linux/macOS
# or
.venv\Scripts\activate     # Windows

Running the Application

# Development mode with auto-reload
uv run uvicorn src.app.main:app --reload

# Or using FastAPI CLI
uv run fastapi dev src/app/main.py

Docker

Build and Run Locally

# Build the image
docker build -t fastapi-devops-pipeline .

# Run the container
docker run -p 8000:80 fastapi-devops-pipeline

Using Docker Hub Image

docker run -p 8000:80 <your-dockerhub-username>/fastapi-devops-pipeline:latest

Access the application at http://localhost:8000

Infrastructure Deployment

1. Configure Terraform Variables

cd infrastructure/terraform

# Copy the example file
cp terraform.tfvars.example terraform.tfvars

# Edit with your values
# - my_public_ip: Your IP in CIDR notation (e.g., "203.0.113.1/32")
# - ssh_public_key_path: Path to your SSH public key

2. Configure Ansible Inventory

cd infrastructure/ansible

# Copy the example file
cp inventory-example.ini inventory.ini

# The deploy script will update the IP automatically

3. Deploy

# Make the script executable
chmod +x scripts/deploy.sh

# Run full deployment
./scripts/deploy.sh

The deployment script will:

  1. βœ… Provision AWS infrastructure with Terraform
  2. βœ… Retrieve the EC2 public IP
  3. βœ… Update Ansible inventory
  4. βœ… Wait for SSH availability
  5. βœ… Configure the server with Ansible
  6. βœ… Deploy the application in Docker

API Reference

Endpoint Method Description
/ GET Welcome message
/health GET Health check endpoint
/metrics GET System metrics (CPU, memory, uptime)
/dashboard GET Interactive Gradio dashboard
/docs GET OpenAPI documentation (Swagger UI)
/redoc GET OpenAPI documentation (ReDoc)

Example Response: /metrics

{
  "uptime_seconds": 3600.25,
  "memory_used_mb": 512.45,
  "memory_total_mb": 2048.00,
  "cpu_percent": 15.5,
  "system": "Linux",
  "architecture": ["64bit", "ELF"],
  "timestamp": "2025-01-15T10:30:00+00:00"
}

Testing

# Run all tests
uv run pytest

# Run with verbose output
uv run pytest -v

# Run with coverage
uv run pytest --cov=src/app

# Linting
uv run ruff check src/

# Format code
uv run ruff format src/

# Run pre-commit checks manually
uv run pre-commit run --all-files

Makefile Commands

Run make help to see all available commands:

make install      # Install production dependencies
make install-dev  # Install dev dependencies + pre-commit hooks
make test         # Run tests
make test-cov     # Run tests with coverage
make lint         # Check code with ruff
make format       # Format code with ruff
make run          # Run FastAPI locally
make docker-build # Build Docker image
make docker-run   # Run Docker container
make deploy       # Full infrastructure deployment
make pre-commit   # Run pre-commit hooks
make clean        # Remove cache files

Project Structure

fastapi-devops-pipeline/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── ci-cd.yaml              # CI/CD pipeline configuration
β”œβ”€β”€ infrastructure/
β”‚   β”œβ”€β”€ ansible/
β”‚   β”‚   β”œβ”€β”€ inventory.ini           # Server inventory (gitignored)
β”‚   β”‚   β”œβ”€β”€ inventory-example.ini
β”‚   β”‚   β”œβ”€β”€ playbook.yaml           # Configuration playbook
β”‚   β”‚   └── requirements.yml        # Ansible collections
β”‚   └── terraform/
β”‚       β”œβ”€β”€ ec2.tf                  # EC2 instance
β”‚       β”œβ”€β”€ key_pair.tf             # SSH key pair
β”‚       β”œβ”€β”€ main.tf                 # Provider configuration
β”‚       β”œβ”€β”€ outputs.tf              # Output values
β”‚       β”œβ”€β”€ security_group.tf       # Security group rules
β”‚       β”œβ”€β”€ variables.tf            # Variable definitions
β”‚       └── terraform.tfvars.example
β”œβ”€β”€ scripts/
β”‚   └── deploy.sh                   # Full deployment script
β”œβ”€β”€ src/
β”‚   └── app/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ main.py                 # FastAPI application entry
β”‚       β”œβ”€β”€ routes.py               # API routes
β”‚       β”œβ”€β”€ schemas.py              # Pydantic models
β”‚       └── dashboard.py            # Gradio dashboard
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ conftest.py                 # Pytest fixtures
β”‚   └── test_routes.py              # API endpoint tests
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .pre-commit-config.yaml         # Pre-commit hooks
β”œβ”€β”€ .python-version
β”œβ”€β”€ Dockerfile                      # Multi-stage Docker build
β”œβ”€β”€ LICENSE
β”œβ”€β”€ Makefile                        # Development commands
β”œβ”€β”€ pyproject.toml                  # Project dependencies
β”œβ”€β”€ uv.lock                         # Locked dependencies
└── README.md

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Ensure all pre-commit hooks pass (uv run pre-commit run --all-files)
  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

Note: Pre-commit hooks will automatically run on every commit. If any hook fails, the commit will be rejected and you'll need to fix the issues before committing again.

License

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

About

A DevOps learning sandbox demonstrating an end-to-end CI/CD pipeline for a FastAPI service, from build and test through containerization and deployment, using Terraform, Ansible, and GitHub Actions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published