Skip to main content
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
}

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.

Build docs developers (and LLMs) love