Documentation Index Fetch the complete documentation index at: https://mintlify.com/terraform-aws-modules/terraform-aws-ecs/llms.txt
Use this file to discover all available pages before exploring further.
This example demonstrates the ECS Managed Instances capacity provider, which allows ECS to manage EC2 instance fleets directly without requiring a separate Auto Scaling Group.
What is created
ECS cluster with a Managed Instances capacity provider
EC2 instance fleet managed by ECS (instance requirements-based selection)
IAM roles: infrastructure role and node role with instance profile
Security group for managed instances
ECS service running on the managed fleet
VPC with private/public subnets
Code
module "ecs_cluster" {
source = "terraform-aws-modules/ecs/aws//modules/cluster"
name = local . name
capacity_providers = {
mi-example = {
managed_instances_provider = {
instance_launch_template = {
instance_requirements = {
instance_generations = [ "current" ]
cpu_manufacturers = [ "intel" , "amd" ]
memory_mib = {
max = 8192
min = 1024
}
vcpu_count = {
max = 4
min = 1
}
}
network_configuration = {
subnets = module.vpc.private_subnets
}
storage_configuration = {
storage_size_gib = 30
}
}
}
}
}
default_capacity_provider_strategy = {
mi-example = {
weight = 100
base = 1
}
}
# Security group for managed instances
vpc_id = module . vpc . vpc_id
security_group_ingress_rules = {
alb-http = {
from_port = local.container_port
description = "Service port"
referenced_security_group_id = module.alb.security_group_id
}
}
security_group_egress_rules = {
all = { cidr_ipv4 = "0.0.0.0/0" , ip_protocol = "-1" }
}
tags = local . tags
}
module "ecs_service" {
source = "terraform-aws-modules/ecs/aws//modules/service"
name = local . name
cluster_arn = module . ecs_cluster . arn
# Run on Managed Instances
requires_compatibilities = [ "MANAGED_INSTANCES" ]
launch_type = "EC2"
network_mode = "awsvpc"
cpu = 512
memory = 512
container_definitions = {
(local . container_name ) = {
image = "public.ecr.aws/aws-containers/ecsdemo-frontend:776fd50"
cpu = 256
memory = 256
portMappings = [{
name = local.container_name
containerPort = local.container_port
protocol = "tcp"
}]
readonlyRootFilesystem = false
}
}
load_balancer = {
service = {
target_group_arn = module.alb.target_groups[ "ex-ecs" ].arn
container_name = local.container_name
container_port = local.container_port
}
}
subnet_ids = module . vpc . private_subnets
vpc_id = module . vpc . vpc_id
security_group_ingress_rules = {
alb_port = {
from_port = local.container_port
description = "Service port"
referenced_security_group_id = module.alb.security_group_id
}
}
security_group_egress_rules = {
all = { cidr_ipv4 = "0.0.0.0/0" , ip_protocol = "-1" }
}
tags = local . tags
}
Key highlights
Instance requirements : Instead of specifying instance types, you specify CPU/memory/generation requirements and ECS selects matching instance types.
No ASG required : ECS Managed Instances handles the fleet lifecycle — no separate terraform-aws-autoscaling module needed.
Security group on the cluster : The cluster module creates the security group for the managed instances (unlike EC2 ASG where the SG is on the ASG).
requires_compatibilities = ["MANAGED_INSTANCES"] : Use this on the service module to target managed instances.
Managed Instances Guide Detailed guide with IAM role requirements and configuration options.
EC2 Autoscaling Example Alternative approach using EC2 Auto Scaling Groups.