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
| Variable | Type | Purpose |
|---|---|---|
name | string | ECS cluster name |
capacity_providers | list(string) | Capacity providers: "EC2" or "FARGATE" |
default_capacity_provider_strategy | object | Default provider weight and base capacity |
autoscaling_group_settings | object | ASG min/max/desired capacity |
container_insights_enabled | bool | Enable CloudWatch Container Insights |
instance_type | string | EC2 instance type for container hosts |
on_demand_percentage | number | Percent on-demand vs spot (0-100) |
Outputs
| Output | Use Case |
|---|---|
cluster_id | Cluster ID for task/service registration |
cluster_arn | ARN for IAM policy references |
cluster_name | Cluster name for CLI/API calls |
autoscaling_group_id | ASG 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