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.
// 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
- Docker imageFlask API with a health-check route, pipenv-locked
- VPC networkpublic and private subnets, IGW, NAT, route tables
- Security groupstasks accept traffic only from the load balancer group
- ALBpublic subnets, IP target group, health checks, listener
- Fargate servicetask definition from a JSON template, private subnets
- IAMexecution role for image pull and logging
- Alarms gateCPU at or above 85% scales up; at or below 10% scales down
- Auto scalingmin 3, max 6 tasks, 60s cooldown
- 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.