Launch Types: Fargate and EC2

ECS can place your tasks on serverless Fargate or on EC2 instances you manage.

A launch type decides where your tasks actually run.

Fargate versus EC2 launch typesFargate (serverless)You define the taskTasks (AWS runs the hosts)No servers to patch or scalePay per vCPU + memory / secondEC2 launch typeYou manage container instancesTasksEC2 container instances (yours)Pay for EC2 hours; more control
Fargate removes host management; EC2 gives you control over the instances and can be cheaper at steady scale.

Fargate

Serverless. You never see a server — AWS provisions the compute for each task. Great default for most workloads.

EC2

You run a fleet of EC2 container instances and ECS packs tasks onto them. More control, more responsibility, often cheaper at steady high scale.

Example

Example · bash
# Run a task on Fargate
aws ecs run-task \
  --cluster my-cluster \
  --launch-type FARGATE \
  --task-definition web:1 \
  --network-configuration 'awsvpcConfiguration={subnets=[subnet-abc],assignPublicIp=ENABLED}'

When to use it

  • A startup uses Fargate so engineers never SSH into EC2 hosts, patch AMIs, or manage instance fleets.
  • A high-volume ad-tech platform uses the EC2 launch type with GPU instances so containers can run CUDA inference workloads that Fargate does not support.
  • A cost-conscious team mixes Fargate Spot for stateless workers and EC2 On-Demand for stateful services that need consistent placement.

More examples

Launch a Task on Fargate

Starts a task with the FARGATE launch type; AWS provisions the compute automatically with no instances to manage.

Example · bash
aws ecs run-task \
  --cluster demo \
  --launch-type FARGATE \
  --task-definition worker:2 \
  --network-configuration \
    'awsvpcConfiguration={subnets=[subnet-1],securityGroups=[sg-1],assignPublicIp=DISABLED}'

Launch a Task on EC2 Launch Type

Starts a task on the EC2 launch type, constrained to g4dn GPU instances for CUDA-based workloads.

Example · bash
aws ecs run-task \
  --cluster ec2-cluster \
  --launch-type EC2 \
  --task-definition gpu-worker:1 \
  --placement-constraints \
    '[{"type":"memberOf","expression":"attribute:ecs.instance-type =~ g4dn.*"}]'

Mix Fargate and Fargate Spot

Splits tasks 75% onto Fargate Spot (cheaper, interruptible) and 25% onto standard Fargate for baseline stability.

Example · bash
aws ecs create-service \
  --cluster demo \
  --service-name spot-workers \
  --task-definition worker:2 \
  --desired-count 4 \
  --capacity-provider-strategy \
    '[{"capacityProvider":"FARGATE_SPOT","weight":3},{"capacityProvider":"FARGATE","weight":1}]'

Discussion

  • Be the first to comment on this lesson.