Fargate vs. ECS

Difficulty: Intermediate

Question

What is AWS Fargate, how does it relate to ECS and EKS, and when would you choose Fargate over EC2-backed containers?

Answer

AWS Fargate is a serverless compute engine for containers: you specify CPU and memory for a task or pod, and AWS runs it on infrastructure you never see, so there are no EC2 instances to patch, scale, or bin-pack. Fargate is not an alternative to ECS; it is one of the ways ECS (and EKS) runs containers. ECS is the orchestrator that decides what to run and keeps it running; Fargate or EC2 is where it runs.

The pieces and how they fit

  • Amazon ECS: AWS's container orchestrator. You define a task definition (images, CPU, memory, ports, IAM role, logging) and a service that keeps N tasks running behind a load balancer.
  • Amazon EKS: managed Kubernetes, the alternative orchestrator.
  • Capacity options: the EC2 launch type (your own instances in a cluster), Fargate (serverless), and External (ECS Anywhere on your own servers).

With Fargate, each task runs in its own isolated microVM with its own kernel, so tasks do not share a host OS with other customers or with each other.

Fargate vs EC2-backed containers

AspectFargateEC2 launch type
Servers to manageNoneYou patch, scale, and right-size instances
BillingPer second for requested vCPU and memory (1-minute minimum)Per instance, whether or not containers fill it
ScalingScale tasks onlyScale tasks and the instance fleet (capacity providers help)
Task sizeUp to 16 vCPU and 120 GB memoryAny instance size
GPUsNot supportedSupported
Host accessNo SSH, no privileged containers, no daemons on the hostFull control
Networkingawsvpc only: each task gets its own ENI and private IPawsvpc, bridge, or host
Cost at steady high utilisationHigher per unitLower, especially with Reserved Instances or Savings Plans

When to choose Fargate

  • Small teams that do not want to run a fleet, and most stateless web services and APIs.
  • Spiky or bursty workloads, scheduled jobs, and batch tasks that would leave EC2 instances idle.
  • Workloads that need stronger isolation per task.
  • Fargate Spot for interruption-tolerant work (queue consumers, CI runners) at a large discount, with a two-minute warning before a task is stopped.

When EC2-backed containers are better

  • GPU workloads (ML inference) or very large tasks.
  • Very steady, high-utilisation workloads where packing containers onto reserved instances is cheaper.
  • Needs like daemon agents on every host, custom kernels, or privileged containers.

Gotchas

  • With awsvpc, every task consumes an IP in your subnet; small subnets run out of IPs during scale-out.
  • Fargate tasks in private subnets need a NAT gateway or VPC endpoints (ECR, S3, CloudWatch Logs) to pull images and ship logs.
  • Ephemeral storage defaults to 20 GiB (configurable up to 200 GiB) and disappears when the task stops; use EFS for persistence.
  • On EKS, Fargate runs one pod per microVM and does not support DaemonSets, so logging and monitoring agents need sidecars or built-in Fluent Bit.
Where Fargate sits in the container stack. ECS or EKS orchestrates; Fargate or EC2 provides the compute underneath.
  1. Your container images: Stored in Amazon ECR or another registry.
  2. Task definition or pod spec: CPU, memory, ports, IAM role, logging.
  3. Orchestrator: ECS or EKS: Schedules, restarts, and scales tasks.
  4. Compute: Fargate or EC2: Fargate hides servers; EC2 gives you the instances.
  5. VPC networking: Each Fargate task gets its own ENI and IP.

Code examples

Fargate task definition for an API service

{
  "family": "orders-api",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "512",
  "memory": "1024",
  "runtimePlatform": { "cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX" },
  "executionRoleArn": "arn:aws:iam::111122223333:role/ecsTaskExecutionRole",
  "taskRoleArn": "arn:aws:iam::111122223333:role/orders-api-task",
  "containerDefinitions": [{
    "name": "api",
    "image": "111122223333.dkr.ecr.ap-south-1.amazonaws.com/orders-api:1.4.2",
    "portMappings": [{ "containerPort": 8080 }],
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "/ecs/orders-api",
        "awslogs-region": "ap-south-1",
        "awslogs-stream-prefix": "api"
      }
    }
  }]
}

Fargate needs awsvpc networking and a valid CPU/memory combination at task level. The execution role pulls images and writes logs; the task role is what your application code uses to call AWS.

Key points

  • Fargate is serverless compute for containers; ECS and EKS are orchestrators that can use it
  • Billed per second for requested vCPU and memory; no instances to manage
  • Fargate requires awsvpc networking; each task gets its own ENI
  • No GPUs, privileged containers, or host daemons on Fargate
  • Fargate Spot is discounted with a two-minute interruption notice

Common follow-up questions

  • What is the difference between the task execution role and the task role?
  • How would you autoscale an ECS service running on Fargate?
  • When would EC2-backed ECS be cheaper than Fargate?
  • What limitations does EKS on Fargate have compared with EKS on EC2 nodes?

Interview tips

  • Correct the framing early: Fargate is a compute option for ECS and EKS, not a competitor to ECS
  • Discuss cost trade-offs at different utilisation levels; it shows practical judgement
  • Mention awsvpc IP consumption or the execution role vs task role distinction as real-world details

Concepts covered

AWS Fargate, Amazon ECS, Launch Types, Task Definitions, Fargate Spot

More AWS & Cloud Fundamentals questions