Sub topic 4
What Fargate manages and what you still own
This chapter assumes you know how to build a Docker image and run a container. It also assumes you are familiar with IAM, VPC subnets, and security groups, and that you have read the previous chapter on ECS clusters, tasks, and services. Here you will see where AWS's responsibility ends and yours begins when you run on Fargate.
The Fargate model in one picture
Fargate is a compute engine for ECS. In ECS you declare what to run: a task definition and a service. Fargate then supplies the compute that runs it. You do not create, patch, or scale EC2 instances. You also do not register instances into a cluster.
Each Fargate task runs in its own isolated environment. The task gets its own kernel, CPU, memory, and elastic network interface (ENI). It shares none of these with other tasks, including tasks from the same service. This is the main difference from the EC2 launch type, where many tasks share one instance and its operating system.
What AWS manages
- Capacity. You never size or scale a pool of instances. When a task needs to start, Fargate finds capacity for it.
- Host operating system and agent. AWS patches the underlying host and runs the ECS agent for you. You cannot log in to the host.
- Isolation. Each task runs in a dedicated environment, so you do not need to reason about noisy neighbors on a shared host.
- Placement. The scheduler places each task in one of the subnets you provide. You choose subnets, and so availability zones. You do not choose a specific machine.
- Platform versions. Fargate publishes platform versions, which bundle the runtime environment, kernel, and container runtime. The default
LATESTvalue moves to newer platform versions over time, and new tasks pick up the change.
What you still own
Removing the servers does not remove the rest of the stack. You are still responsible for the following.
- The image. You build it, scan it, patch its base layers, and rebuild when a vulnerability is published. Fargate patches the host. It does not patch the libraries inside your container.
- The task definition. You choose the CPU and memory, the ports, the environment, and the health check.
- IAM. You decide what the task execution role may do (pull images, write logs, read secrets at startup) and what the task role may do (calls your application code makes to AWS).
- Networking. You choose subnets, security groups, and whether a task gets a public IP. A misconfigured private subnet with no route to ECR or a NAT gateway is a very common cause of tasks that never start.
- Application behavior. Your code must handle
SIGTERM, expose a health endpoint, and write logs to stdout and stderr. - Scaling and deployment policy. You set the desired count, auto scaling targets, and the deployment circuit breaker.
- Cost. You are billed for the vCPU and memory you request for each task, whether or not the container uses them.
Constraints that come with the model
The managed model is convenient, but it comes with limits you should know before you design around it.
| Area | Fargate behavior |
|---|---|
| Host access | No SSH. Use ECS Exec to open a shell in a container. |
| Network mode | Only `awsvpc` is supported. Each task gets its own ENI and private IP. |
| CPU and memory | Set at the task level, and only from a fixed list of valid combinations. |
| Privileged mode | Not available. Containers cannot run with `privileged: true`. |
| Host devices and GPUs | Not available on Fargate. |
| Docker socket | Not available, so you cannot run Docker-in-Docker builds. |
| Storage | Ephemeral storage per task. It is deleted when the task stops. Use EFS for data that must persist. |
| Task requirement | You must declare `FARGATE` in `requiresCompatibilities` in the task definition. |
Check the current AWS documentation for exact limits such as ephemeral storage size, because they change over time.
How the model appears in a task definition
The Fargate-specific settings show up as a few required fields. The excerpt below shows only those fields. The full task definition appears in the next chapter.
{
"family": "web-app",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "256",
"memory": "512",
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
}
}If you leave out networkMode or set it to anything other than awsvpc, registration fails or the task cannot be used with Fargate. If you choose ARM64 for cpuArchitecture, your image must be built for ARM. Otherwise the container exits at startup with an exec format error. Graviton tasks are one of the cost levers discussed later in this tutorial.
Choosing Fargate versus EC2 launch type
Fargate is the better default when you want less operational work. The EC2 launch type can still win in some cases. Use these criteria to decide.
- Choose Fargate when you want no instance patching, your workloads are web services or batch jobs of modest size, and your traffic varies enough that paying per task is acceptable.
- Choose EC2 when you need GPUs, privileged containers, host-level access, very large tasks, or the lowest unit price for steady high utilization where you can pack instances densely.
You can mix both in one cluster by using capacity providers. A later chapter covers Fargate Spot.
Failure modes to expect
Because you do not manage hosts, failures look different from those on EC2.
- Task fails to start. Typical causes are image pull errors (no route to ECR, or missing execution role permissions), an invalid CPU and memory pair, or a secret the execution role cannot read. The task's
stoppedReasonin the console or indescribe-tasksusually names the cause. - Task stops unexpectedly. The platform can retire a task, for example during Fargate infrastructure maintenance, and the service scheduler starts a replacement. Design for tasks to disappear at any time, and keep at least two tasks in different availability zones for services that matter.
- Container killed for memory. If a container exceeds its memory limit, the kernel stops it with an out-of-memory error, and the exit code is 137. Raising the task memory is the fix, along with checking for leaks.
- Slow starts. Fargate pulls the image on every new task, so large images slow scale-out. Keep images small.
You can inspect why a task stopped with the AWS CLI.
aws ecs describe-tasks \
--cluster demo \
--tasks <task-id> \
--query "tasks[0].{status:lastStatus,reason:stoppedReason,containers:containers[].{name:name,exit:exitCode,reason:reason}}"Replace <task-id> with a task ID from aws ecs list-tasks --cluster demo --desired-status STOPPED. Stopped tasks stay visible for a limited time, so check soon after a failure.
Summary of the ownership boundary
- AWS owns the hosts, their patching, the isolation of each task, and the capacity pool.
- You own the image, the task definition, IAM, networking, application behavior, scaling policy, and the bill.
Exercise
A Fargate task keeps stopping within seconds with the reason "CannotPullContainerError". The task runs in a private subnet with no NAT gateway. Name two responsibilities from your side of the boundary that could be the cause, and one fix for each.
Show answer
First, networking: the subnet has no route to ECR, so the image pull cannot complete. Fix it by adding a NAT gateway or VPC endpoints for ECR (the ecr.api and ecr.dkr interface endpoints) and the S3 gateway endpoint that holds image layers. Second, IAM: the task execution role may lack permission to pull from ECR. Fix it by attaching the AmazonECSTaskExecutionRolePolicy managed policy, or equivalent ECR permissions, to the execution role. A third possible cause is a wrong image name or tag, which you fix in the task definition.
Exercise
Your Fargate image runs fine on your x86 laptop. After you set cpuArchitecture to ARM64 in the task definition to save cost, tasks stop at startup with "exec format error". What is the cause and how do you fix it?
Show answer
The image was built for x86_64, and the ARM64 task cannot run its binaries. Rebuild the image for linux/arm64 (for example with docker buildx build --platform linux/arm64, or a multi-architecture manifest), push it to ECR, and update the task definition to use it.