Skip to main content

ECS Cluster Module

Deploy an AWS Elastic Container Service (ECS) cluster with EC2 or Fargate capacity providers.

What You'll Build

  • ECS cluster with configurable capacity providers
  • EC2 launch template for container hosts
  • Auto Scaling Group for EC2 capacity
  • CloudWatch Container Insights monitoring
  • Optional ECS Service Autoscaling policies

How to Use

module "ecs_cluster" {
source = "github.com/nnthanh101/terraform-aws/modules/ecs"

name = "app-cluster"

# EC2 capacity provider
capacity_providers = ["EC2"]

default_capacity_provider_strategy = {
capacity_provider = "EC2"
weight = 100
base = 1
}

autoscaling_group_settings = {
max_size = 10
min_size = 2
desired_capacity = 3
}

# Container Insights
container_insights_enabled = true

tags = {
Environment = "prod"
Service = "platform"
}
}

Key Variables

VariableTypePurpose
namestringECS cluster name
capacity_providerslist(string)Capacity providers: "EC2" or "FARGATE"
default_capacity_provider_strategyobjectDefault provider weight and base capacity
autoscaling_group_settingsobjectASG min/max/desired capacity
container_insights_enabledboolEnable CloudWatch Container Insights
instance_typestringEC2 instance type for container hosts
on_demand_percentagenumberPercent on-demand vs spot (0-100)

Outputs

OutputUse Case
cluster_idCluster ID for task/service registration
cluster_arnARN for IAM policy references
cluster_nameCluster name for CLI/API calls
autoscaling_group_idASG ID for scaling policy attachment

Integration

  • ECS Tasks: Register task definitions and create services in the cluster
  • CloudWatch: Container Insights provides metrics and logs
  • Autoscaling: Attach Application Autoscaling policies to services
  • Load Balancer: ALB target groups reference the ECS service
  • IAM: Task execution role for pulling images and accessing secrets

Source Reference

Module: terraform-aws/modules/ecs