Containers on ECS with Fargate

More and more apps turn up as container images, and somebody has to run them. This ECS Fargate tutorial shows you how, with Amazon ECS scheduling the containers and keeping them healthy while Fargate runs them without any EC2 instances for you to patch or scale.

You’ll create a cluster, a task definition for nginx and a service of two tasks behind the load balancer from Lesson 20, and then read the container logs. After that you’ll fix the most common ECS ticket of all, CannotPullContainerError, and a task definition that Fargate refuses to accept.

TimeYou needBy the end you can
60 minutes (load balancer and two small Fargate tasks for under an hour)You need saat-vpc with saat-web-sg (Lessons 8 to 10) and your laptop signed in as saat-admin. It costs about USD 0.15 for two 0.25 vCPU tasks, their public IPv4 addresses and the load balancer for an hour.Create an ECS cluster, a task execution role, a task definition and a Fargate service with a load balancer and deployment circuit breaker. Read the container logs, and troubleshoot image pull and task definition errors.

What it is, in one line

Amazon ECS runs and manages containers, and with Fargate AWS provides the compute for each task, so you never look after the servers underneath.

In Simple Words

The dabbawala system

Mumbai’s dabbawalas deliver thousands of lunch boxes every day. Each box is packed the same way at home, which is your container image, and a card on it says what goes inside and where it’s going, which is the task definition.

The dabbawala service makes sure the right number of boxes reach the right offices each day, and if one goes missing another is sent, much like an ECS service. With Fargate you don’t own any bicycles or trains either; you say how many boxes and the transport turns up.

If the box never leaves home because the road is closed, though, nothing reaches the office, and that’s CannotPullContainerError.

Engineer's View

How ECS and Fargate work

A task definition is a versioned JSON recipe listing the images, CPU and memory, port mappings, environment variables, secrets, log configuration and two roles. The execution role lets ECS pull images and write logs, while the task role is what your app’s code uses to call AWS. A service keeps a set number of tasks running, registers them with load balancer target groups (type ip for Fargate) and rolls out new revisions with rolling updates, and the deployment circuit breaker rolls back any deployment whose tasks keep failing.

Fargate tasks use awsvpc networking, which gives each task its own ENI, private IP and security groups. To pull images a task needs a route to the registry, through a public IP in a public subnet, a NAT gateway, or VPC endpoints for ECR (ecr.api, ecr.dkr and the S3 gateway) plus logs. Fargate only supports fixed CPU and memory pairs, such as 0.25 vCPU with 0.5 to 2 GB.

Logs go to CloudWatch Logs through the awslogs driver. ECS Exec gets you a shell inside a running container through Session Manager, and Container Insights adds metrics per task.

How it works, step by step

This is how a request gets to nginx in the lab.

1The ALB receives the request

saat-web-alb listens on port 80 in the public subnets, the same as in Lesson 20.

ECS Fargate request flow: browser, ALB, IP target group, Fargate task

2The target group holds task IPs

For Fargate the target type is ip. When the service starts a task, ECS registers that task’s private IP, and when it stops one, ECS drains it and takes it out.

3The task answers

Each task’s security group only allows port 80 from saat-web-sg, and nginx logs every request to CloudWatch Logs.

4Learn the four words

You’ll hear these in almost every ECS conversation:

ECS concepts: task definition, task, service, execution role and task role

If you remember Lesson 21, a service is to tasks what an Auto Scaling group is to instances, and a task definition revision is a bit like a launch template version.

✅ Checkpoint: You can explain a task definition, a task and a service, the two roles, and what a Fargate task needs in order to pull its image.

Hands-on lab: an ECS Fargate tutorial behind a load balancer

Before you run any command, double-check it

Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.

Sign in and load saat-vpc:

cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'
$v = Get-Content C:\aws-lab\saat-vpc.json | ConvertFrom-Json

1Create the cluster, role, logs and task definition

Paste this whole block:

aws ecs create-cluster --cluster-name saat-ecs --tags key=course,value=saatvedha-aws --query cluster.status --output text
'{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ecs-tasks.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }' | Set-Content ecs-trust.json
$execRole = aws iam create-role --role-name saat-ecs-exec-role --assume-role-policy-document file://ecs-trust.json --query Role.Arn --output text
aws iam attach-role-policy --role-name saat-ecs-exec-role --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
aws logs create-log-group --log-group-name /ecs/saat-web
aws logs put-retention-policy --log-group-name /ecs/saat-web --retention-in-days 7
@'
{
  "family": "saat-web",
  "networkMode": "awsvpc",
  "requiresCompatibilities": [ "FARGATE" ],
  "cpu": "256",
  "memory": "512",
  "runtimePlatform": { "operatingSystemFamily": "LINUX", "cpuArchitecture": "X86_64" },
  "executionRoleArn": "EXEC_ROLE_ARN",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "public.ecr.aws/nginx/nginx:stable-alpine",
      "essential": true,
      "portMappings": [ { "containerPort": 80, "protocol": "tcp" } ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": { "awslogs-group": "/ecs/saat-web", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "web" }
      }
    }
  ],
  "tags": [ { "key": "course", "value": "saatvedha-aws" } ]
}
'@ -replace 'EXEC_ROLE_ARN', $execRole | Set-Content taskdef.json
Start-Sleep 10
aws ecs register-task-definition --cli-input-json file://taskdef.json --query "taskDefinition.[family,revision,cpu,memory]" --output text

You should see this:

ACTIVE
saat-web        1       256     512

Revision 1 of saat-web is the recipe: nginx from Amazon ECR Public, 0.25 vCPU and 0.5 GB, with logs going to /ecs/saat-web. Every change creates a new revision, so there’s always something to roll back to.

2Create the load balancer and the task security group

Paste this whole block (about three minutes):

$taskSg = aws ec2 create-security-group --group-name saat-ecs-task-sg --description "Fargate tasks, 80 from the ALB" --vpc-id $v.VpcId --query GroupId --output text
aws ec2 authorize-security-group-ingress --group-id $taskSg --protocol tcp --port 80 --source-group $v.WebSg | Out-Null
$tg = aws elbv2 create-target-group --name saat-ecs-tg --protocol HTTP --port 80 --vpc-id $v.VpcId --target-type ip --health-check-path / --health-check-interval-seconds 10 --healthy-threshold-count 2 --query "TargetGroups[0].TargetGroupArn" --output text
aws elbv2 modify-target-group-attributes --target-group-arn $tg --attributes Key=deregistration_delay.timeout_seconds,Value=30 | Out-Null
$alb, $dns = (aws elbv2 create-load-balancer --name saat-web-alb --type application --scheme internet-facing --subnets $v.PublicA $v.PublicB --security-groups $v.WebSg --tags Key=course,Value=saatvedha-aws --query "LoadBalancers[0].[LoadBalancerArn,DNSName]" --output text) -split '\s+'
aws elbv2 wait load-balancer-available --load-balancer-arns $alb
aws elbv2 create-listener --load-balancer-arn $alb --protocol HTTP --port 80 --default-actions "Type=forward,TargetGroupArn=$tg" | Out-Null
$dns

You should see something like this:

saat-web-alb-1234567890.ap-south-1.elb.amazonaws.com

3Run the service

Paste this whole block (the service usually settles in two to four minutes):

aws ecs create-service --cluster saat-ecs --service-name saat-web --task-definition saat-web --desired-count 2 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[$($v.PublicA),$($v.PublicB)],securityGroups=[$taskSg],assignPublicIp=ENABLED}" --load-balancers "targetGroupArn=$tg,containerName=web,containerPort=80" --deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true},maximumPercent=200,minimumHealthyPercent=100" --health-check-grace-period-seconds 30 --propagate-tags SERVICE --tags key=course,value=saatvedha-aws | Out-Null
aws ecs wait services-stable --cluster saat-ecs --services saat-web
aws ecs describe-services --cluster saat-ecs --services saat-web --query "services[0].[status,desiredCount,runningCount]" --output text
(Invoke-WebRequest -UseBasicParsing "http://$dns/").Content | Select-String -Pattern "<title>"

You should see something like this:

ACTIVE  2       2
<title>Welcome to nginx!</title>

You now have two containers running on Fargate without a single EC2 instance in your account. The tasks sit in public subnets with public IPs only so they can pull the image from the internet, and their security group still lets in nothing except the load balancer.

ECS Fargate tutorial lab output: service running, nginx page, container logs
Service running, the nginx page and container logs. Something like this.

4Read the container logs

Paste this whole block:

1..3 | ForEach-Object { Invoke-WebRequest -UseBasicParsing "http://$dns/" | Out-Null }
Start-Sleep 10
aws logs tail /ecs/saat-web --since 5m --format short | Select-Object -Last 2

You should see something like this:

2026-10-20T23:05:12 10.20.1.34 - - [20/Oct/2026:17:35:12 +0000] "GET / HTTP/1.1" 200 615 "-" "ELB-HealthChecker/2.0"
2026-10-20T23:05:14 10.20.2.71 - - [20/Oct/2026:17:35:14 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-IN) WindowsPowerShell/5.1"

Each task writes to its own stream under the web prefix, and the health checker’s requests come from the load balancer’s private IPs. If you open ECS in the console you can see the service, its tasks and the deployments.

Amazon ECS service saat-web with two running healthy Fargate tasks in two Availability Zones
The saat-web service in ECS: two Fargate tasks running and healthy, one in each zone.

✅ Checkpoint: saat-web is ACTIVE with 2 of 2 tasks running, the load balancer returns the nginx page and the container logs are in /ecs/saat-web. Keep everything for the break lab.

Break It, Then Fix It

Here’s the ECS failure you’ll run into most often, and a task definition Fargate won’t accept.

1Break it: tasks that can’t reach the registry

Deploy the service without public IPs, give it two minutes and then read why the new tasks stopped:

aws ecs update-service --cluster saat-ecs --service saat-web --network-configuration "awsvpcConfiguration={subnets=[$($v.PublicA),$($v.PublicB)],securityGroups=[$taskSg],assignPublicIp=DISABLED}" --force-new-deployment | Out-Null
Start-Sleep 120
$stopped = aws ecs list-tasks --cluster saat-ecs --service-name saat-web --desired-status STOPPED --query "taskArns[0]" --output text
aws ecs describe-tasks --cluster saat-ecs --tasks $stopped --query "tasks[0].stoppedReason" --output text
(Invoke-WebRequest -UseBasicParsing "http://$dns/").StatusCode

You should see something like this:

CannotPullContainerError: pull image manifest has been retried 7 time(s): failed to resolve ref public.ecr.aws/nginx/nginx:stable-alpine: failed to do request: Head "https://public.ecr.aws/v2/nginx/nginx/manifests/stable-alpine": dial tcp 99.83.145.10:443: i/o timeout
200
ECS problems: CannotPullContainerError i/o timeout and invalid Fargate CPU memory combination
Both problems and their fixes. Something like this.

With no public IP, NAT gateway or VPC endpoints, the new tasks have no road to the registry. The site still answers 200 because the old tasks carry on running, since the service only replaces them once new ones are healthy. Leave it for about ten minutes and the circuit breaker marks the deployment FAILED and rolls it back.

Fix: give the tasks a path to the registry. In this lab that means putting the public IP back, and in production you’d use private subnets with ECR and logs endpoints or a NAT gateway:

aws ecs update-service --cluster saat-ecs --service saat-web --network-configuration "awsvpcConfiguration={subnets=[$($v.PublicA),$($v.PublicB)],securityGroups=[$taskSg],assignPublicIp=ENABLED}" --force-new-deployment | Out-Null
aws ecs wait services-stable --cluster saat-ecs --services saat-web
aws ecs describe-services --cluster saat-ecs --services saat-web --query "services[0].deployments[].[status,rolloutState,runningCount]" --output text

You should see something like this:

PRIMARY COMPLETED       2

2Break it: a CPU and memory pair Fargate doesn’t offer

Try registering a revision with 0.25 vCPU and 4 GB of memory:

(Get-Content taskdef.json -Raw) -replace '"memory": "512"', '"memory": "4096"' | Set-Content taskdef-bad.json
aws ecs register-task-definition --cli-input-json file://taskdef-bad.json

You’ll get this error:

An error occurred (ClientException) when calling the RegisterTaskDefinition operation: No Fargate configuration exists for given values: 256 CPU, 4096 memory. See the Amazon ECS documentation for the valid values.

Fargate sells fixed sizes, so 0.25 vCPU allows 0.5, 1 or 2 GB and 4 GB needs at least 0.5 vCPU. Teams usually hit this when someone raises memory for an app that’s running out of it and forgets about CPU. Fix: choose a valid pair, for example cpu 512 with memory 4096, or stay on 256 with 2048.

Now clean up. Remove the service before the cluster and the load balancer before the target group:

aws ecs update-service --cluster saat-ecs --service saat-web --desired-count 0 | Out-Null
aws ecs delete-service --cluster saat-ecs --service saat-web --force | Out-Null
aws ecs wait services-inactive --cluster saat-ecs --services saat-web
aws ecs list-task-definitions --family-prefix saat-web --query taskDefinitionArns --output text | ForEach-Object { $_ -split '\s+' } | Where-Object { $_ } | ForEach-Object { aws ecs deregister-task-definition --task-definition $_ | Out-Null }
aws ecs delete-cluster --cluster saat-ecs | Out-Null
aws elbv2 delete-load-balancer --load-balancer-arn $alb
aws elbv2 wait load-balancers-deleted --load-balancer-arns $alb
Start-Sleep 30
aws elbv2 delete-target-group --target-group-arn $tg
aws ec2 delete-security-group --group-id $taskSg
aws iam detach-role-policy --role-name saat-ecs-exec-role --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
aws iam delete-role --role-name saat-ecs-exec-role
aws logs delete-log-group --log-group-name /ecs/saat-web
Remove-Item ecs-trust.json, taskdef.json, taskdef-bad.json
.\Find-Waste.ps1
.\Find-LabResources.ps1

✅ Checkpoint: You read a CannotPullContainerError from a stopped task and fixed the network path, and watched Fargate refuse an invalid CPU and memory pair. The service, cluster, load balancer, roles and logs are gone.

This happened at work

The deployment that pulled from Docker Hub

A travel company’s ECS services pulled their base images straight from Docker Hub through a NAT gateway. One Monday morning a sale kicked off a scaling event that launched 60 new tasks across services at the same time.

That’s when Docker Hub’s anonymous pull rate limit kicked in. New tasks failed with CannotPullContainerError mentioning “toomanyrequests”, the services couldn’t scale and checkout was slow for 40 minutes. When the month’s bill came in, the NAT gateway’s data processing charges were also oddly high because of all the repeated image pulls.

These days they copy images into private Amazon ECR repositories (using ECR pull-through cache for public images), pull through ECR VPC endpoints from private subnets, scan images on push, and alarm on any ECS service event that mentions CannotPullContainerError.

Common mistakes and troubleshooting

MistakeWhat happensWhat to do
No route from tasks to the registryCannotPullContainerError: i/o timeoutPublic IP, NAT gateway, or ECR and logs endpoints
Pulling from Docker Hub at scaleRate limits during scale-outPrivate ECR with pull-through cache
Confusing execution role and task roleImage pull or app API calls failExecution role for ECS, task role for your code
Invalid CPU and memory pairClientException: No Fargate configuration existsUse a supported pair
No circuit breakerBroken deployments loop foreverEnable deploymentCircuitBreaker with rollback
Task security group open to the internetContainers reachable directlyAllow only the load balancer’s security group

Interview questions

Click a question to open it. Each one is phrased the way an interviewer would ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.

Fresher level

1. What’s the difference between running ECS on EC2 and running it on Fargate, and when would you pick each?

Why they ask this: They want to know you understand who manages what in each option, because it decides how much patching and scaling your team ends up doing.

How you might answer: With the EC2 launch type I manage the container instances myself, patching and scaling included. With Fargate, AWS gives each task its own isolated compute and I only define the CPU, memory and networking.

If the team doesn’t want to look after servers, I’d go with Fargate.

2. Can you explain in simple words what a task definition is?

Why they ask this: They’re checking you know the core building block of ECS, since you can’t run or troubleshoot a container without reading one.

How you might answer: It’s a versioned JSON description of how to run my containers, listing the images, CPU, memory, ports, environment variables, secrets, logging and IAM roles.

I think of it as the recipe ECS follows each time it starts a task.

3. A new team member is confused about the execution role and the task role in ECS. How would you explain the difference?

Why they ask this: They want to see you’re clear on container permissions, because mixing up these two roles is one of the most common ECS mistakes.

How you might answer: I’d tell them the execution role is used by ECS itself to pull the image and send logs, and the task role is used by the application inside the container when it calls AWS services, say reading from S3.

So when the image won’t pull I look at the execution role, and when the app gets AccessDenied I look at the task role.

2–5 years experience

4. Your tasks keep stopping with CannotPullContainerError. What would you check?

Why they ask this: They want a logical troubleshooting order, since this error has several common causes and they’d like to hear you check the network first.

How you might answer: I’d start with the network path, because the task needs a public IP, a NAT gateway, or ECR endpoints plus the S3 gateway endpoint. Then I’d make sure the image name and tag are right.

After that I’d check the execution role’s registry permissions, the ECR repository policy, and the registry’s rate limits if the image comes from a public registry.

5. Walk me through how an ECS service rolls out a new version without taking the app down.

Why they ask this: They want to know you understand safe deployments, because a bad rollout is one of the main causes of outages.

How you might answer: By default ECS does a rolling update controlled by minimum healthy percent and maximum percent, and it uses the load balancer health checks to decide whether new tasks are good. I’d switch on the deployment circuit breaker with rollback so a broken version rolls itself back.

For riskier apps I’d use blue/green deployments, either with CodeDeploy or ECS native blue/green.

6. You need to look inside a running Fargate container to debug a problem. How would you get a shell?

Why they ask this: They’re checking you can debug without SSH, because Fargate has no server to log in to, so this shows real hands-on experience.

How you might answer: I’d use ECS Exec. That means enabling it on the service, giving the task role the SSM messages permissions and then running aws ecs execute-command.

It runs on Session Manager underneath, so I don’t have to open any ports.

Senior level

7. Say you’re asked to build a container platform for 40 microservices. How would you design it?

Why they ask this: They want platform thinking across networking, security, scaling and delivery, and whether you’d design something teams can actually run day to day.

How you might answer: I’d run ECS on Fargate in private subnets across three zones, with an ALB doing host or path routing. Images would live in ECR with scanning and a pull-through cache, and VPC endpoints would keep the traffic private.

Every service would have its own task role and read its secrets from Secrets Manager. I’d add service auto scaling, circuit breakers, Container Insights, central logs and CI/CD that promotes images from one environment to the next.

8. An ECS service keeps replacing its tasks every few minutes. How would you investigate?

Why they ask this: They’re testing deeper troubleshooting, because restart loops have several possible causes and they want to see you look at the evidence before guessing.

How you might answer: I’d read the service events and the stopped task reasons first, then the container exit codes and logs. Next I’d compare the load balancer health check settings with how long the app takes to start, because a short grace period kills tasks that are still booting.

I’d also look at memory limits in case of OOM kills, and at whether the app fails because a dependency isn’t ready when it starts.

Quick quiz

Q1. What does a Fargate task need to pull a public image?

  1. Nothing
  2. A route to the internet or registry (public IP, NAT or endpoints)
  3. An EC2 instance
  4. A key pair
Show answer

B — No route means CannotPullContainerError.

Q2. Which role does ECS use to pull images and write logs?

  1. Task role
  2. Execution role
  3. Instance role
  4. Service-linked role only
Show answer

B — The execution role is for ECS itself.

Q3. Why did the site keep answering during the bad deployment?

  1. Caching
  2. Old tasks kept running until new ones were healthy
  3. The ALB served a copy
  4. Fargate retried forever
Show answer

B — Minimum healthy percent 100 keeps old tasks.

Q4. What does the deployment circuit breaker do?

  1. Stops the cluster
  2. Fails and rolls back deployments whose tasks keep failing
  3. Limits CPU
  4. Blocks traffic
Show answer

B — It prevents endless failed deployments.

Q5. Why was 256 CPU with 4096 memory refused?

  1. Too cheap
  2. Fargate only offers fixed CPU and memory pairs
  3. Wrong Region
  4. The image was too big
Show answer

B — 4 GB needs at least 0.5 vCPU.

Quick recap

  • Task definition is the recipe, a task is a running copy, a service keeps tasks running.
  • The execution role pulls images and writes logs; the task role is for your app.
  • Fargate tasks need a path to the registry: public IP, NAT or endpoints.
  • Use the deployment circuit breaker and load balancer health checks.
  • Fargate supports fixed CPU and memory pairs.

Next lesson

Lesson 40: RDS vs EC2 for SQL Server → Phase 3 begins here. You’ll compare running SQL Server on RDS and on EC2 and work out which one suits real workloads.

Further reading: ECS Fargate tutorial

Want to go deeper into ECS Fargate tutorial? Read What is Amazon Elastic Container Service? and Amazon ECS stopped task error messages. You can also see the full plan for this course on the AWS Cloud Administration course page.

Scroll to Top

This website uses cookies

We use a few cookies to keep this site working. If you allow it, we also use Google Analytics to count visits so we can improve our lessons. You can accept all, deny the optional ones, or choose what to allow.