Cloud / IaC · Freelance

Auto-scaled Containerised API on ECS Fargate

Terraform stack running a Dockerised Flask API on ECS Fargate behind an ALB, with CPU-alarm step scaling between 3 and 6 tasks.

  • Terraform
  • ECS Fargate
  • ALB
  • VPC / NAT
  • CloudWatch
  • IAM
  • ECR
  • Docker
  • Python / Flask

// Overview

A complete Terraform project provisions a VPC with public and private subnets, internet and NAT gateways, an ALB with target group and listener, an ECS cluster, a Fargate task definition and service. The container is a small Flask REST API with pipenv-locked dependencies. Step-scaling policies driven by CloudWatch CPU alarms scale the service; logs go to CloudWatch.

// The problem

A repeatable, scalable, cheap-to-operate way to host a containerised web API without managing servers.

// Flow

Auto-scaled Containerised API on ECS Fargate flow1. Docker image → 2. VPC network → 3. Security groups → 4. ALB → 5. Fargate service → 6. IAM → 7. Alarms → 8. Auto scaling → 9. Logs01Docker image02VPC network03Security groups04ALB05Fargate service06IAM07 · GATEAlarms08Auto scaling09Logs
  1. Docker imageFlask API with a health-check route, pipenv-locked
  2. VPC networkpublic and private subnets, IGW, NAT, route tables
  3. Security groupstasks accept traffic only from the load balancer group
  4. ALBpublic subnets, IP target group, health checks, listener
  5. Fargate servicetask definition from a JSON template, private subnets
  6. IAMexecution role for image pull and logging
  7. Alarms gateCPU at or above 85% scales up; at or below 10% scales down
  8. Auto scalingmin 3, max 6 tasks, 60s cooldown
  9. Logscontainer log group in CloudWatch

// Design decisions & guardrails

  • Security-group-to-security-group rules instead of CIDRs; NAT gives private egress.
  • File-per-concern layout: network, alb, ecs, auto_scaling, security, roles, logs.
  • Image, CPU, memory and task count exposed as variables.

// Outcome

Server-less hosting that scales itself, delivered as plain Terraform with a Makefile wrapper.

// Related