Container Instances

With the EC2 launch type you run a fleet of EC2 hosts that ECS schedules tasks onto.

A container instance is an EC2 instance that has joined an ECS cluster. ECS packs tasks onto these instances based on their available CPU, memory, and ports.

What you manage

  • The EC2 instance type, count, and AMI (use the ECS-optimized AMI).
  • Patching, scaling, and the underlying Docker daemon.

In return you get full control of the host and can achieve high task density at lower cost.

Example

Example · bash
# List the EC2 hosts registered to a cluster
aws ecs list-container-instances --cluster prod

# Describe their remaining CPU/memory
aws ecs describe-container-instances --cluster prod --container-instances <id>

When to use it

  • An ML team registers P3 GPU EC2 instances as ECS container instances so their GPU-accelerated inference containers can run on the EC2 launch type.
  • A cost-optimised team buys Reserved EC2 instances, registers them as container instances, and lets ECS pack multiple tasks onto each host.
  • An ops engineer lists all container instances in a cluster to find one that is failing health checks before draining and replacing it.

More examples

Launch EC2 Instance into ECS Cluster

Launches a t3.medium EC2 instance with the ECS agent pre-installed (ECS-optimised AMI) and points it at the target cluster via user data.

Example · bash
aws ec2 run-instances \
  --image-id ami-0c55b159cbfafe1f0 \
  --instance-type t3.medium \
  --iam-instance-profile Name=ecsInstanceRole \
  --user-data '#!/bin/bash
echo ECS_CLUSTER=my-ec2-cluster >> /etc/ecs/ecs.config' \
  --count 1

List Container Instances in a Cluster

Lists the ARNs of all active container instances registered in the cluster, useful for auditing fleet size.

Example · bash
aws ecs list-container-instances \
  --cluster my-ec2-cluster \
  --status ACTIVE \
  --output table

Describe Container Instance Resources

Shows the remaining CPU units and memory on a specific container instance to check available capacity for new tasks.

Example · bash
aws ecs describe-container-instances \
  --cluster my-ec2-cluster \
  --container-instances arn:aws:ecs:us-east-1:123456789012:container-instance/abc123 \
  --query 'containerInstances[0].{ec2Id:ec2InstanceId,cpu:remainingResources[?name==`CPU`].integerValue|[0],memory:remainingResources[?name==`MEMORY`].integerValue|[0]}'

Discussion

  • Be the first to comment on this lesson.