An open API service indexing awesome lists of open source software.

https://github.com/ronihdzz/aws-lambda-fastapi-template

Professional template for building serverless REST APIs with FastAPI, Docker, and AWS Lambda. Includes modular architecture, real database testing, and full CI/CD integration.
https://github.com/ronihdzz/aws-lambda-fastapi-template

aws-lambda ci-cd cloud docker fastapi github-actions mangum microservices mongodb postgresql pydantic python rest-api serverless sqlalchemy template testing

Last synced: 2 months ago
JSON representation

Professional template for building serverless REST APIs with FastAPI, Docker, and AWS Lambda. Includes modular architecture, real database testing, and full CI/CD integration.

Awesome Lists containing this project

README

          

# AWS Lambda FastAPI Template

[![English](https://img.shields.io/badge/πŸ‡ΊπŸ‡Έ-English-blue)](README.md) [![EspaΓ±ol](https://img.shields.io/badge/πŸ‡ͺπŸ‡Έ-EspaΓ±ol-red)](README.es.md)

| Environment | Coverage |
|-----------|----------|
| main | ![Coverage Badge](https://github.com/ronihdzz/aws-lambda-fastapi/blob/artifacts/main/latest/coverage.svg) |
| development | ![Coverage Badge](https://github.com/ronihdzz/aws-lambda-fastapi/blob/artifacts/development/latest/coverage.svg) |

## πŸ“‹ Template Description

This is a complete template for developing REST APIs with **FastAPI** designed to be deployed on **AWS Lambda** using **Mangum** as a reverse proxy. The template implements a complete **serverless** approach, providing modular architecture, standardized error handling, automated testing against real databases, and a comprehensive CI/CD stack with Docker.

### πŸš€ **Serverless & AWS Lambda Context**

The template is optimized for **serverless architectures** using AWS Lambda:
- **Mangum** acts as an adapter/reverse proxy that allows FastAPI to function as a Lambda function
- **Serverless Computing**: No server management, automatic scaling, pay-per-use
- **API Gateway Integration**: Compatible with AWS API Gateway for routing and authentication
- **Cold Start Optimization**: Structure optimized to minimize cold start time

## πŸ—οΈ Architecture and Project Structure

### Directory Structure

```
aws-lambda-fastapi/
β”œβ”€β”€ src/ # Main source code
β”‚ β”œβ”€β”€ api/ # API layer - Routers and endpoints
β”‚ β”‚ β”œβ”€β”€ v1/ # API versioning
β”‚ β”‚ β”œβ”€β”€ routers.py # Main router registration
β”‚ β”‚ └── endpoints.py # Base endpoints (health check, etc.)
β”‚ β”œβ”€β”€ core/ # Central configuration
β”‚ β”‚ β”œβ”€β”€ settings/ # Environment-specific configurations
β”‚ β”‚ β”œβ”€β”€ exceptions.py # Domain-specific exceptions
β”‚ β”‚ └── internal_codes.py # Domain internal codes
β”‚ β”œβ”€β”€ shared/ # Shared utilities
β”‚ β”‚ β”œβ”€β”€ middlewares/ # Custom middlewares
β”‚ β”‚ β”œβ”€β”€ base_responses.py # Standardized responses
β”‚ β”‚ β”œβ”€β”€ base_internal_codes.py # Base internal codes
β”‚ β”‚ β”œβ”€β”€ base_exceptions.py # Base exceptions
β”‚ β”‚ └── utils_dates.py # Date utilities
β”‚ β”œβ”€β”€ db/ # Database layer
β”‚ β”‚ β”œβ”€β”€ postgresql/ # PostgreSQL configuration
β”‚ β”‚ └── mongo/ # MongoDB configuration
β”‚ β”œβ”€β”€ tests/ # Test suite
β”‚ └── main.py # Application entry point
β”œβ”€β”€ docker_images/ # Custom Docker images
β”‚ └── testing/ # Testing configuration
β”œβ”€β”€ .github/workflows/ # CI/CD pipelines
└── docs/ # Project documentation
```

## πŸ”§ Main Components

### 1. **Standardized Response System** (`@/shared/base_responses.py`)

This template implements a consistent response system using an **Envelope Response** pattern:

```python
class EnvelopeResponse(BaseModel):
success: bool
message: str
data: dict[str, Any] | list | None = None
trace_id: str | None = None
```

**Conventions:**
- All responses follow the same format
- Automatic Pydantic model serialization handling
- Trace_id inclusion for debugging
- Structured error responses with internal codes

### 2. **Internal Code System** (`@/shared/base_internal_codes.py`)

Standardized error code system for tracking and debugging:

```python
class CommonInternalCode(InternalCodeBase):
UNKNOWN = 100, "Unknown error"
PYDANTIC_VALIDATIONS_REQUEST = 8001, "Failed Pydantic validations on request"
```

**Conventions:**
- Unique numeric codes for each error type
- Clear error description
- Extensible for domain-specific codes in `@/core/internal_codes.py`

### 3. **Error Handling Middleware** (`@/shared/middlewares/`)

#### CatcherExceptions
Main middleware that catches all exceptions:
- FastAPI HTTPException handling
- SQLAlchemy NoResultFound handling
- Custom exception handling
- Automatic conversion to standardized response format

#### CatcherExceptionsPydantic
Specific middleware for Pydantic validation errors:
- Catches request body validation errors
- Consistent validation error formatting

### 4. **Configuration System** (`@/core/settings/`)

Environment-based configuration using Pydantic Settings:

```
settings/
β”œβ”€β”€ base.py # Base configuration
β”œβ”€β”€ local.py # Local development
β”œβ”€β”€ development.py # Development environment
β”œβ”€β”€ staging.py # Staging environment
β”œβ”€β”€ production.py # Production environment
└── testing.py # Testing configuration
```

**Conventions:**
- Typed environment variables
- Automatic configuration validation
- Environment-specific configuration
- Multi-database support

### 5. **Multi-Database System** (`@/db/`)

Template prepared for multiple database managers with **functional examples**:

#### PostgreSQL (`@/db/postgresql/`)
```
postgresql/
β”œβ”€β”€ base.py # Base class for SQLAlchemy models
β”œβ”€β”€ connection.py # Connection configuration
β”œβ”€β”€ models/
β”‚ └── public/ # 'public' schema
β”‚ └── book.py # Example model
└── __init__.py # Main exports
```

- **SQLAlchemy 2.0**: Modern ORM with modern syntax
- **Typed Models**: Full type hints and validation
- **Singleton Connection**: Reusable and efficient

#### MongoDB (`@/db/mongo/`)
```
mongo/
β”œβ”€β”€ base.py # Base class for documents
β”œβ”€β”€ connection.py # Configured MongoDB client
β”œβ”€β”€ models/ # Document models
└── __init__.py # Main exports
```

- **PyMongo**: Official MongoDB driver
- **Pydantic Integration**: Models with automatic validation
- **Async Ready**: Prepared for asynchronous operations

**Conventions:**
- Models organized by schemas/collections
- Singleton connection pattern
- Environment-specific configuration
- **Extensible**: Easy to add Redis, DynamoDB, etc.

### 6. **Modular API Structure** (`@/api/`)

Versioning system and modular organization by domain:

```
api/
β”œβ”€β”€ v1/ # API Version 1
β”‚ └── books/ # Domain module (example)
β”‚ β”œβ”€β”€ endpoints.py # Defines REST endpoints
β”‚ β”œβ”€β”€ repositories.py # Database access
β”‚ β”œβ”€β”€ schema.py # Pydantic models (DTOs)
β”‚ └── services.py # Business logic
β”œβ”€β”€ routers.py # Global router registration
└── endpoints.py # Base endpoints (health, index)
```

**Module Conventions:**
- **endpoints.py**: Route definition and input validations
- **services.py**: Business logic, orchestration
- **repositories.py**: Data access, specific queries
- **schema.py**: DTOs (Data Transfer Objects) with Pydantic

**Scalability**: You can add more modules (`users/`, `orders/`, etc.) following the same structure.

### 7. **Testing System** (`@/tests/`)

Complete automated testing suite:

```
tests/
β”œβ”€β”€ common/ # Common utilities for tests
β”œβ”€β”€ utils/ # Testing utilities
β”œβ”€β”€ v1/ # Version-specific tests
└── __init__.py # Database configuration for tests
```

**Conventions:**
- **Separate testing database**: Automatic configuration via `environment_testing`
- **Reusable fixtures**: Common utilities in `tests/common/`
- **Automated coverage**: Integrated in CI/CD with detailed reports
- **Tests organized by version**: Modular structure by API version
- **Automatic setup**: The `prepare_database()` script creates necessary schemas and tables each time the runner starts

### 8. **Comprehensive Docker Stack** (`@/docker_images/`)

The project comprehensively organizes Docker images for different purposes:

#### **Testing Docker** (`@/docker_images/testing/`)
Specialized configuration for testing against **real databases** (not mocks):

- **`Dockerfile.testing`**: Optimized image for running tests
- **`ci.env.sh`**: Script that configures environment variables for CI/CD
- **`entrypoint.sh`**: Orchestrates test execution and report generation

**Database Selection in CI:**
```bash
# In ci.env.sh you can select which databases to spin up
export POSTGRESQL_URL="${GITHUB_DATABASE_POSTGRESQL:-$POSTGRESQL_URL}"
export MONGO_URL="${GITHUB_DATABASE_MONGODB:-$MONGO_URL}"
export REDIS_URL="${GITHUB_DATABASE_REDIS:-$REDIS_URL}"
```

Tests run against **real instances** of PostgreSQL, MongoDB, and Redis in the GitHub Actions runner.

#### **Build and Deploy Docker**
- **`docker_images/build/`**: Optimized construction image
- **`docker_images/deploy/`**: Final image for AWS Lambda deployment

## πŸš€ Complete CI/CD Pipelines

### **Main GitHub Actions Flow**

The pipeline executes a complete and robust sequence:

#### **1. πŸ” Linting (Pre-commit)**
```yaml
- Executes pre-commit for linting (configured in .pre-commit-config.yaml)
- Code formatting with Black
- Linting with Flake8
- Type checking with MyPy
- Import and structure validation
```

#### **2. πŸ§ͺ Testing with Real Databases**
```yaml
services:
postgres: # PostgreSQL 13 on port 5432
mongodb: # MongoDB 4.4 on port 27017
redis: # Redis 6 on port 6379
```
- Spins up real database services
- Runs tests against real instances (not mocks)
- Generates detailed coverage reports

#### **3. πŸ“Š Coverage and Badges**
- **Generation**: `coverage run`, `coverage xml`, `coverage-badge`
- **Archival**: Uses `ronihdzz/git-archive-action@v3` to save artifacts
- **Storage**: Reports are stored in dedicated `artifacts` branch
- **Badges**: Dynamic coverage % badges are generated and displayed in README

#### **4. πŸ—οΈ Build and Deploy**
- Docker image construction optimized for Lambda
- Push to Amazon ECR (Elastic Container Registry)
- Automatic deploy to AWS Lambda
- Automatic versioning by environment (dev/staging/prod)

### **Environment Configuration**

```yaml
BRANCH_ENV_MAP:
"main": "prod"
"development": "dev"
"staging": "stg"
```

Each branch automatically maps to its corresponding environment.

## πŸ“¦ Dependencies and Technologies

### Core Dependencies
- **FastAPI**: Modern and fast web framework
- **Mangum**: Adapter for AWS Lambda
- **Pydantic**: Data validation and settings
- **SQLAlchemy**: ORM for relational databases
- **PyMongo**: MongoDB driver
- **Loguru**: Advanced logging

### Development Dependencies
- **Pytest**: Testing framework
- **Coverage**: Code coverage
- **MyPy**: Type checking
- **Pre-commit**: Pre-commit hooks

## πŸ› οΈ Development Commands

### Run the project locally
```bash
uvicorn main:app --reload --port=9595
```

### Run tests
```bash
pytest src/tests/ -v --cov=src
```

### Run pre-commit hooks
```bash
pre-commit run --all-files
```

### Type checking
```bash
mypy src/
```

### Testing with Docker
```bash
docker-compose -f docker-compose.yml up testing
```

## πŸ” Lambda Local Testing

To test the Lambda function locally:

```bash
curl -X POST "http://localhost:9100/2015-03-31/functions/function/invocations" \
-H "Content-Type: application/json" \
-d '{
"version": "2.0",
"routeKey": "GET /",
"rawPath": "/",
"rawQueryString": "",
"headers": {},
"requestContext": {
"http": {
"method": "GET",
"path": "/",
"sourceIp": "127.0.0.1"
}
},
"isBase64Encoded": false
}'
```

## 🎯 **Template Advantages**

### **For Development**
- **Serverless Ready**: Optimized for AWS Lambda with Mangum
- **Multi-Database**: Native support for PostgreSQL, MongoDB, Redis
- **Type Safety**: Full TypeScript-like experience with MyPy
- **Error Handling**: Robust error handling system with internal codes

### **For Testing**
- **Real Databases**: Tests against real databases, not mocks
- **Automated Setup**: Automatic creation of schemas and test data
- **Coverage Tracking**: Detailed reports with automatic badges
- **Docker Isolated**: Completely isolated testing environment

### **For CI/CD**
- **Full Pipeline**: Linting β†’ Testing β†’ Coverage β†’ Build β†’ Deploy
- **Multi-Environment**: Automatic support for dev/staging/prod
- **Quality Gates**: Pre-commit hooks and automatic validations
- **Artifact Management**: Automatic storage of reports and badges

### **For Production**
- **AWS Optimized**: Configured for AWS Lambda + API Gateway
- **Environment Config**: Typed configuration per environment
- **Monitoring Ready**: Structured logging with Loguru + Sentry
- **Scalable Architecture**: Modular structure easy to scale