Terraformterraformawsecsfargateproduction-ready

ECS Fargate Service

Terraform configuration for deploying a containerized service on AWS ECS Fargate with ALB, auto-scaling, and CloudWatch logging.


title: "ECS Fargate Service" description: "Terraform configuration for deploying a containerized service on AWS ECS Fargate with ALB, auto-scaling, and CloudWatch logging." category: "Terraform" language: "hcl" tags: ["terraform", "aws", "ecs", "fargate", "production-ready"] createdAt: "2025-02-20" updatedAt: "2025-03-10"

Overview

A complete ECS Fargate deployment with Application Load Balancer, CloudWatch logging, health checks, deployment circuit breaker for automatic rollback, and CPU-based auto-scaling.

Code

module "api" {
  source          = "./modules/ecs-fargate"
  app_name        = "api"
  environment     = "production"
  container_image = "123456.dkr.ecr.us-east-1.amazonaws.com/api:v1.0"
  desired_count   = 3
}

Notes

  • Fargate eliminates EC2 instance management; you only specify CPU/memory per task
  • Deployment circuit breaker automatically rolls back if new tasks fail health checks
  • Tasks run in private subnets with ALB in public subnets (no direct internet exposure)
  • Container Insights provides detailed CPU, memory, and network metrics per task
  • deregistration_delay = 30 gives in-flight requests time to complete during deployments