EC2 Launch Type vs Fargate
Compare managing EC2 container instances yourself versus using Fargate for serverless container execution and understand the cost tradeoffs.
Two Ways to Run ECS Tasks
ECS offers two launch types that define where your containers run: EC2 launch type runs containers on EC2 instances that you provision and manage within the cluster; Fargate launch type is a serverless compute engine where AWS manages the underlying servers entirely. The task definition and service configuration are almost identical between them—the key difference is infrastructure management responsibility and cost model.
EC2 Launch Type: Full Control
With the EC2 launch type, you register EC2 instances as container instances in the cluster using the ECS Container Agent (pre-installed on ECS-optimised AMIs). You choose instance types, control OS-level settings, apply custom security baselines, and use Spot Instances for cost savings. ECS places tasks across your registered instances based on bin-packing or spread placement strategies. You are responsible for patching, scaling, and maintaining the underlying EC2 fleet.
# Launch an ECS-optimised EC2 instance that joins the cluster
# User data for ECS container agent
#!/bin/bash
echo ECS_CLUSTER=MyAppCluster >> /etc/ecs/ecs.config
echo ECS_ENABLE_CONTAINER_METADATA=true >> /etc/ecs/ecs.config