EC2 Launch Type vs Fargate
Compare managing EC2 container instances yourself versus using Fargate for serverless container execution and understand the cost tradeoffs.
EC2 Launch Type vs Fargate is a free AWS Solutions Architect lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.configFargate: Serverless Containers
With Fargate, you specify CPU and memory per task, and AWS provisions the underlying compute invisibly. There are no EC2 instances to register, patch, or scale—AWS manages all of it. Tasks get their own isolated kernel (via Firecracker microVMs), providing stronger security isolation than EC2 container instances where multiple tasks share the same host. Fargate requires the awsvpc network mode, meaning each task gets its own ENI and VPC IP address.
aws ecs run-task \
--cluster 'MyAppCluster' \
--task-definition 'myapp-task:5' \
--launch-type FARGATE \
--network-configuration '{
"awsvpcConfiguration": {
"subnets": ["subnet-aaa111"],
"securityGroups": ["sg-xyz"],
"assignPublicIp": "ENABLED"
}
}'Cost Model Comparison
The cost models are fundamentally different. EC2 launch type: pay for the underlying EC2 instances (regardless of whether tasks are running), but can use Reserved Instances or Savings Plans for discount and Spot Instances for up to 90% savings on interruptible workloads. Fargate: pay only for vCPU and memory consumed per task per second—no idle cost between tasks. For spiky or batch workloads, Fargate is often cheaper; for consistently high-utilisation workloads, EC2 with Reserved Instances can be cheaper.
Fargate Spot for Cost Savings
Fargate Spot provides Fargate compute at significantly reduced prices (up to 70% off) in exchange for the possibility of interruption when AWS needs capacity back. Fargate Spot is ideal for batch processing, CI/CD pipelines, and development/staging environments. Configure a capacity provider strategy to mix Fargate (on-demand) and Fargate Spot: use base=1 on Fargate to ensure at least one guaranteed task, and weight values to split remaining capacity across both.
aws ecs create-service \
--cluster 'MyAppCluster' \
--service-name 'BatchService' \
--task-definition 'batch-task:1' \
--desired-count 10 \
--capacity-provider-strategy \
'capacityProvider=FARGATE,weight=1,base=1' \
'capacityProvider=FARGATE_SPOT,weight=4'When to Choose EC2 Launch Type
Choose the EC2 launch type when you need: GPU instances (Fargate has limited GPU support), specific instance types not available in Fargate, custom OS configuration (kernel tuning, custom drivers), network performance requiring placement groups or enhanced networking, Windows containers (EC2 supports Windows Server containers; Fargate supports Windows but with limitations), or when running consistently at high utilisation where EC2 Reserved Instances provide significant cost advantage over Fargate per-second billing.
When to Choose Fargate
Choose Fargate when you want to: eliminate EC2 management overhead (no OS patching, no cluster scaling), pay for only what you use per second, run spiky or batch workloads that don't justify idle EC2 capacity, achieve stronger task isolation via Firecracker microVMs, or accelerate development by focusing purely on application code. Fargate is the default recommendation for new ECS workloads on the SAA-C03 exam unless a specific EC2 requirement is stated.
ECS Capacity Providers
Capacity providers abstract the compute layer from ECS services. For EC2, a capacity provider links to an Auto Scaling Group—ECS scales the ASG automatically when tasks can't be placed due to insufficient capacity. For Fargate, FARGATE and FARGATE_SPOT are built-in capacity providers. Defining a capacity provider strategy on a service allows a mix of on-demand and Spot/Fargate Spot, giving fine-grained control over cost and availability trade-offs per workload.
Comparing Operational Overhead
Operational overhead differs significantly between launch types. EC2 launch type requires: AMI selection and updates, EC2 Auto Scaling configuration, OS patching via SSM or custom scripts, container instance capacity management, and instance-level security hardening. Fargate eliminates all of the above—AWS handles patching the Fargate platform, no instances to manage, no cluster capacity planning. However, Fargate provides less flexibility for OS-level customisation and network performance tuning.
Task CPU and Memory Sizing for Fargate
Fargate task definitions must specify CPU and memory from a fixed set of combinations. CPU values: 256, 512, 1024, 2048, 4096, 8192, 16384 (in vCPU units where 1024 = 1 vCPU). Memory must fall within allowed ranges per CPU value—for example, 256 CPU allows 512-2048 MB memory. Containers within the task share the task's CPU and memory ceiling. Right-sizing is important: Fargate bills exactly for what you specify, so over-provisioning wastes money with no actual performance benefit if workloads are CPU-bound.
# Valid Fargate combinations
# 256 CPU (.25 vCPU): 512, 1024, or 2048 MB
# 512 CPU (.5 vCPU): 1024 to 4096 MB
# 1024 CPU (1 vCPU): 2048 to 8192 MB
# 2048 CPU (2 vCPU): 4096 to 16384 MB
# 4096 CPU (4 vCPU): 8192 to 30720 MBECS Exec for Debugging
ECS Exec lets you open an interactive shell session directly in a running container (both EC2 and Fargate) without SSH or bastion hosts. It uses AWS Systems Manager Session Manager under the hood. Enable it on the service and use the execute-command CLI to start a bash session. This is invaluable for debugging production issues in containers. The task role needs ssmmessages:CreateControlChannel and related SSM permissions.
aws ecs execute-command \
--cluster 'MyAppCluster' \
--task 'abc123def456' \
--container 'myapp' \
--interactive \
--command '/bin/bash'Quick Check
Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.
Lesson Recap
In this lesson you learned: EC2 launch type gives full control over instance types and OS configuration at the cost of managing the underlying fleet—ideal for GPU, specialised hardware, or consistently high-utilisation workloads, Fargate eliminates infrastructure management with per-second task billing and strong Firecracker isolation—ideal for most web/API workloads and batch jobs, and Fargate Spot reduces costs by up to 70% for interruption-tolerant workloads. Next up we explore Amazon ECR for storing and pulling container images.
Frequently asked questions
Is the “EC2 Launch Type vs Fargate” lesson free?
Yes — the full text of “EC2 Launch Type vs Fargate” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.
What will I learn in “EC2 Launch Type vs Fargate”?
Compare managing EC2 container instances yourself versus using Fargate for serverless container execution and understand the cost tradeoffs. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AWS Solutions Architect?
No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “EC2 Launch Type vs Fargate” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AWS Solutions Architect lesson?
Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.