Deploying FastAPI with UV, Nginx, and AWS ECS: A Step-by-Step Guide

Prerequisites

  • Intermediate understanding of Python development (Python 3.13 recommended)
  • Basic familiarity with Docker and AWS ECS
  • Docker installed (Docker version 20.10+)
  • Terraform installed (Terraform version 1.0+)
  • AWS CLI installed and configured
  • Minimum 4GB RAM and 10GB disk space

Introduction

FastAPI has become a popular choice for building fast, modern APIs with Python. This enhanced guide takes you through deploying a FastAPI application using the UV package manager, Nginx, and AWS ECS for a production-ready environment. With Docker and Terraform, you’ll create a scalable architecture that’s robust, efficient, and includes comprehensive monitoring and logging.

We’ll use the uv-fastapi-ecs repository as a template. This repository demonstrates:


Repository Overview

The uv-fastapi-ecs repository provides a well-structured starting point for deploying FastAPI applications. Here’s a breakdown of what it offers:

Key Features

Repository Structure

.
├── app/
│   └── main.py                # FastAPI application
├── nginx/
│   ├── nginx.conf.ecs         # Nginx config for ECS
│   └── nginx.conf.local       # Nginx config for local development
├── Dockerfile                 # FastAPI Dockerfile
├── Dockerfile.local           # Nginx Dockerfile for local dev
├── Dockerfile.nginx           # Nginx Dockerfile for ECS
├── docker-compose.yml         # Local development setup
├── terraform/                 # Infrastructure as code for AWS ECS
│   ├── 1-ecr/                 # ECR repositories
│   ├── 2-network/             # VPC and networking
│   ├── 3-ecs/                 # ECS cluster and services
│   ├── 4-monitoring/          # Monitoring and logging setup
└── README.md                  # Repository documentation

Each component is designed to work independently, making it easy to extend or modify.


Local Development

Setting Up

  1. Clone the repository:

    git clone https://github.com/loftwah/uv-fastapi-ecs.git
    cd uv-fastapi-ecs
    
  2. Build and start the services:

    docker compose up --build
    
  3. Access the services:

Key Benefits of Local Development Setup


Deploying to AWS ECS

Terraform Modules

The infrastructure is divided into four Terraform modules:

  1. ECR (Elastic Container Registry):

    • Creates repositories for FastAPI and Nginx images.
    • Lifecycle policies ensure only the latest images are retained.
  2. Networking:

    • Creates a VPC with public and private subnets.
    • Configures Internet and NAT Gateways for outbound traffic.
    • Provides secure networking for ECS tasks.
  3. ECS (Elastic Container Service):

    • Deploys FastAPI and Nginx containers on Fargate.
    • Configures an Application Load Balancer (ALB) to route traffic.
    • Enables monitoring and logging with CloudWatch.
  4. Monitoring:

    • Sets up CloudWatch dashboards and alarms.
    • Configures SNS topics for alert notifications.
    • Centralizes logging for FastAPI and Nginx services.

Prerequisites

Before you begin, ensure you have:

Step-by-Step Deployment

1. Deploy the ECR Module

Navigate to the ECR module directory:

cd terraform/1-ecr

Copy and edit the variables file:

cp terraform.tfvars.example terraform.tfvars

Edit terraform.tfvars to match your environment.

Initialize and apply the Terraform configuration:

terraform init
terraform apply

2. Build and Push Docker Images

Return to the project root:

cd ../../

Run the build and push script:

./build-and-push.sh

This script will build the Docker images and push them to the ECR repositories created in the previous step.

3. Deploy the Networking Module

Navigate to the networking module directory:

cd terraform/2-network

Copy and edit the variables file:

cp terraform.tfvars.example terraform.tfvars

Edit terraform.tfvars to match your environment.

Initialize and apply the Terraform configuration:

terraform init
terraform apply

4. Deploy the ECS Module

Navigate to the ECS module directory:

cd ../3-ecs

Copy and edit the variables file:

cp terraform.tfvars.example terraform.tfvars

Edit terraform.tfvars, ensuring you provide:

Initialize and apply the Terraform configuration:

terraform init
terraform apply

Note: The ECS module depends on outputs from the networking module.

5. Deploy the Monitoring Module

Navigate to the monitoring module directory:

cd ../4-monitoring

Copy and edit the variables file:

cp terraform.tfvars.example terraform.tfvars

Edit terraform.tfvars, ensuring you provide:

Initialize and apply the Terraform configuration:

terraform init
terraform apply

6. Retrieve the Application Load Balancer DNS Name

Navigate back to the ECS module directory:

cd ../3-ecs

Retrieve the ALB DNS name:

terraform output alb_dns_name

Access the application at the ALB DNS name.


Monitoring and Logging

CloudWatch Integration

CloudWatch Dashboards

A CloudWatch dashboard is set up to visualize key metrics:

Accessing the Dashboard:

CloudWatch Alarms

Setting Up Alerts


Scaling and Management

Scaling

Managing ECS Services


Advanced Topics

Security Best Practices

Blue/Green Deployments

CI/CD Pipeline


Conclusion

The uv-fastapi-ecs repository provides everything you need to deploy a scalable, production-ready FastAPI application on AWS. By combining the power of Docker, Terraform, and ECS, and integrating monitoring and logging with CloudWatch, this setup ensures a robust architecture for modern Python development.

Try this out, and feel free to extend it further to suit your needs. If you have questions or want to share your experience, join the discussion in the repository!