AWS Fargate vs EC2: How to Run Docker Containers Without Managing Servers
Running Docker containers in production forces an immediate architectural decision: do you manage the underlying servers yourself, or do you let AWS handle that entirely? AWS Fargate eliminates the EC2 instance management burden, but understanding exactly what that trade-off means — in terms of cost, control, and operational overhead — is what separates a well-architected system from an expensive mistake.
TL;DR — Fargate vs EC2 for Containers
| Dimension | AWS Fargate | EC2 (Self-Managed) |
|---|---|---|
| Server Management | None — AWS owns the host | You patch, scale, and maintain instances |
| Pricing Model | Per vCPU/memory per second (task-level) | Per instance-hour (regardless of utilization) |
| Scaling Granularity | Task-level (individual container) | Instance-level (entire EC2 node) |
| Kernel / OS Access | No access to host OS | Full root access to the instance |
| Startup Time | Slightly slower (cold provisioning) | Faster if instance is already running |
| Spot / Savings Plans | Fargate Spot + Compute Savings Plans | EC2 Spot, Reserved, Savings Plans |
| Best For | Microservices, event-driven, bursty workloads | GPU workloads, high-density, custom AMIs |
What Is AWS Fargate?
Fargate is a serverless compute engine for containers. It works as a launch type for both Amazon ECS (Elastic Container Service) and Amazon EKS (Elastic Kubernetes Service). When you use Fargate, AWS provisions, configures, and scales the underlying compute infrastructure invisibly. You define your container's CPU and memory requirements, and Fargate runs it — full stop.
Think of it this way: with EC2, you rent a warehouse and arrange the shelves yourself. With Fargate, you hand AWS a box and say "store this" — you never see the warehouse.
Analogy: EC2 is like leasing a physical office — you control the layout, furniture, and maintenance schedule. Fargate is like using a co-working space — you just show up with your laptop (container), and the infrastructure is already handled. You pay for the desk you use, not the entire floor.
How Fargate Works Internally
Understanding Fargate's architecture clarifies why it behaves differently from EC2-hosted containers.
- Task Definition: You declare your container image, CPU units, memory (MB), IAM role, networking mode, and logging config in an ECS Task Definition.
- ECS Scheduler: When a task is launched, the ECS control plane sends the task spec to the Fargate fleet.
- Micro-VM Isolation: Fargate provisions a dedicated, single-tenant micro-VM (using AWS Firecracker technology) for each task. No two customer tasks share a host kernel.
- ENI Injection: An Elastic Network Interface (ENI) is injected directly into your VPC, giving the task a private IP address within your subnet — this is the
awsvpcnetwork mode, mandatory for Fargate. - Task Execution: Your container runs. AWS manages the host OS, hypervisor patches, and capacity.
- Billing Starts/Stops: You are billed from when the task starts downloading your container image to when it stops — measured in vCPU-seconds and GB-seconds.
Fargate on ECS vs EKS
Fargate integrates with both orchestrators, but the operational model differs slightly.
- ECS + Fargate: AWS-native orchestration. Simpler setup, tighter AWS service integration (ALB, Service Discovery, CloudMap). No Kubernetes knowledge required.
- EKS + Fargate: Uses Fargate Profiles to route specific pods (matched by namespace/label selectors) to Fargate. The Kubernetes control plane still runs on managed EC2 nodes (the EKS control plane itself). Daemonsets are not supported on Fargate nodes in EKS.
EC2 Launch Type: When You Keep Control
With the EC2 launch type on ECS, you manage a cluster of EC2 instances. The ECS agent runs on each instance and registers it with the cluster. The scheduler then places containers (tasks) onto instances based on available CPU/memory.
This model gives you:
- Access to GPU instances (e.g.,
p3,g4dn) — Fargate does not support GPU workloads. - Custom AMIs with pre-installed agents, kernel modules, or compliance tooling.
- Higher container density per dollar at sustained, predictable load (Reserved Instances).
- Support for ECS Daemonsets (one task per instance) for log shippers, monitoring agents, etc.
Networking Deep-Dive: awsvpc Mode
Fargate mandates the awsvpc network mode. Each Fargate task gets its own ENI and a private IP from your VPC subnet. This has critical implications:
- Security Groups: Applied at the task level, not the instance level — fine-grained control.
- ENI Limits: Your subnet and account have ENI limits. High task counts require subnet CIDR planning. Check the VPC limits documentation for current quotas.
- No Host Networking: You cannot use
hostorbridgenetwork modes with Fargate.
IAM Security Model
Fargate uses two distinct IAM roles — a common source of confusion:
| Role | Purpose | Example ARN |
|---|---|---|
| Task Execution Role | Used by the Fargate agent to pull ECR images, write CloudWatch logs | arn:aws:iam::123456789012:role/ecsTaskExecutionRole |
| Task Role | Used by your application code inside the container to call AWS APIs (S3, DynamoDB, etc.) | arn:aws:iam::123456789012:role/MyAppTaskRole |
Always follow least privilege: the Task Execution Role should only have ecr:GetAuthorizationToken, ecr:BatchGetImage, and logs:CreateLogStream / logs:PutLogEvents permissions at minimum.
Practical Implementation: ECS Fargate Task Definition
Below is a minimal but production-ready ECS Task Definition for Fargate. It defines a single container, assigns CPU/memory, configures CloudWatch logging, and references both IAM roles.
🔽 Click to expand — ECS Fargate Task Definition (JSON)
{
"family": "my-api-task",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/MyAppTaskRole",
"containerDefinitions": [
{
"name": "my-api",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-api:latest",
"portMappings": [
{
"containerPort": 8080,
"protocol": "tcp"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/my-api-task",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"essential": true
}
]
}
Deploying the Service via AWS CLI
🔽 Click to expand — ECS Service Creation (CLI)
# Register the task definition
aws ecs register-task-definition \
--cli-input-json file://task-definition.json \
--region us-east-1
# Create the ECS Service with Fargate launch type
aws ecs create-service \
--cluster my-cluster \
--service-name my-api-service \
--task-definition my-api-task \
--desired-count 2 \
--launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[subnet-0abc1234,subnet-0def5678],securityGroups=[sg-0123456789abcdef0],assignPublicIp=DISABLED}" \
--region us-east-1
Cost Optimization: Fargate Spot
Fargate Spot runs tasks on spare AWS capacity at a significant discount compared to standard Fargate pricing. AWS can interrupt Fargate Spot tasks with a 2-minute warning (via a SIGTERM signal to your container). This makes it ideal for:
- Batch processing jobs
- CI/CD pipeline runners
- Non-critical background workers
You can mix standard Fargate and Fargate Spot using ECS Capacity Provider Strategies — for example, run 70% on Fargate Spot and 30% on standard Fargate for resilience.
Pricing and discount percentages vary. Always check the official Fargate pricing page for current rates.
Decision Framework: Fargate or EC2?
Key Limitations of Fargate to Know
- No GPU support: GPU-accelerated workloads (ML inference, video encoding) require EC2 instances.
- No privileged containers: You cannot run containers in privileged mode on Fargate.
- No host volume mounts: Only EFS (Elastic File System) and ephemeral storage are supported for persistent data. Bind mounts to the host are not available.
- EKS Daemonsets: Not supported on Fargate nodes — use EC2 node groups for cluster-wide agents.
- Ephemeral storage: Each Fargate task gets a default amount of ephemeral storage. You can configure additional ephemeral storage up to a documented maximum — check the ECS Fargate storage documentation for current limits.
Glossary
| Term | Definition |
|---|---|
| Task Definition | A blueprint (JSON) describing your container image, CPU, memory, networking, and IAM roles for ECS. |
| awsvpc Network Mode | ECS network mode that assigns each task its own ENI and VPC IP — mandatory for Fargate. |
| Fargate Spot | Interruptible Fargate capacity at reduced cost, suitable for fault-tolerant workloads. |
| Task Execution Role | IAM role used by the Fargate agent (not your app) to pull images and write logs. |
| Firecracker | AWS open-source VMM (Virtual Machine Monitor) that provides lightweight micro-VM isolation for Fargate tasks. |
Next Steps
- 📘 Official ECS Fargate Developer Guide
- 📘 EKS Fargate Documentation
- 📘 Fargate Pricing Page
- 🔧 Start with a simple ECS Fargate service, validate your networking and IAM setup, then layer in Fargate Spot Capacity Providers for cost optimization.
Comments
Post a Comment