DEV Community

Cover image for How to Deploy FastAPI on AWS ECS Fargate with Zero NAT Gateway Bills ($35/mo Savings)
Muhammad Usman
Muhammad Usman

Posted on

How to Deploy FastAPI on AWS ECS Fargate with Zero NAT Gateway Bills ($35/mo Savings)

πŸ› Architecture Overview

                                  Internet Traffic
                                         β”‚
                                         β–Ό
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚   Application Load Balancer   β”‚ (Port 80 / 443 HTTPS)
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚ Health Check: GET /health (HTTP 200)
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β–Ό                                       β–Ό
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚  ECS Fargate Task (Subnet A)β”‚         β”‚  ECS Fargate Task (Subnet B)β”‚
     β”‚  FastAPI + Docker (Port 8000)β”‚        β”‚  FastAPI + Docker (Port 8000)β”‚
     β”‚  Target Type: IP (awsvpc)   β”‚         β”‚  Target Type: IP (awsvpc)   β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚                                       β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                       β–Ό                                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Amazon ECR  β”‚     β”‚ CloudWatch Log Group β”‚               β”‚ External Database    β”‚
β”‚  Repository  β”‚     β”‚  (/ecs/fastapi-ecs)  β”‚               β”‚ (Supabase/Neon/RDS)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Cost Tip: When developing or testing, use the 1-click destroy action in GitHub Actions to tear down all resources when you finish, spending less than $0.20 total per test.


1. Secure Multi-Stage Dockerfile (Fast & SOC2 Compliant)

# Stage 1: Build Wheels & Compile Dependencies
FROM python:3.11-slim AS builder
WORKDIR /build

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc build-essential && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip \
    && pip wheel --no-cache-dir --no-deps --wheel-dir /build/wheels -r requirements.txt

# Stage 2: Minimal Non-Root Runtime Container
FROM python:3.11-slim AS runner
WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PORT=8000

# Security: Run as non-root user (UID 10001) for SOC2 compliance
RUN groupadd -g 10001 appgroup && \
    useradd -u 10001 -g appgroup -s /bin/sh -m appuser

COPY --from=builder /build/wheels /wheels
RUN pip install --no-cache-dir --upgrade pip \
    && pip install --no-cache-dir /wheels/* \
    && rm -rf /wheels

COPY . /app
RUN chown -R appuser:appgroup /app
USER appuser

EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

2. Terraform ECS Fargate Service ($0 NAT Gateway Architecture)

# ECS Fargate Service in Public Subnet (Saves $35+/month on NAT Gateways)
resource "aws_ecs_service" "app" {
  name            = "fastapi-ecs-dev-service"
  cluster         = aws_ecs_cluster.main.id
  task_definition = aws_ecs_task_definition.app.arn
  desired_count   = 1
  launch_type     = "FARGATE"

  deployment_minimum_healthy_percent = 50
  deployment_maximum_percent         = 200

  network_configuration {
    subnets          = var.public_subnet_ids
    security_groups  = [var.ecs_security_group_id]
    assign_public_ip = true # Direct route to Internet Gateway - $0 NAT Gateway needed!
  }

  load_balancer {
    target_group_arn = var.target_group_arn
    container_name   = "fastapi-ecs-api"
    container_port   = 8000
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸš€ Get the Turnkey Production Boilerplate

Want the complete, ready-to-deploy source code with automated GitHub Actions CI/CD, Route 53 SSL, and CPU auto-scaling?

πŸ‘‰ Grab the complete boilerplate on Gumroad: https://usmanasghar1.gumroad.com/l/fastapi-aws-ecs

Top comments (0)