What Is Amazon ECS and Fargate? Run Docker Without Servers
Swiggy packs each kitchen order in a box. Docker packs your app the same way. ECS is the kitchen manager that runs many boxes. Fargate means AWS owns the kitchen machines — you only bring the boxes.
In this chapter
- Why teams moved from one fat EC2 to many containers
- What cluster, task definition, task and service mean
- Fargate vs EC2 launch type
- ECS vs Lambda vs EKS — when to pick
- Pricing shape and a tiny mental lab path
1. Everyday example first
Flipkart Black Friday traffic spikes. One big EC2 with Node, Redis and cron is hard to scale and hard to replace. Containers let you ship the same image to many small workers and kill them when the sale ends.
Docker packages code + libs into an image. A running image is a container. Amazon ECS (Elastic Container Service) starts, stops and restarts containers on AWS. Fargate is a launch type where you do not manage EC2 for those containers.
2. Core words
Begin with a container image, the packaged code and files the program needs. A container is an instance of that program running from the image. In ECS, a task definition lists the image, CPU, memory, ports and other settings. A task is one running copy of that definition. A service keeps a chosen number of tasks running, while a cluster groups related tasks and services.
Take a practice web service with a desired count of two. The service asks ECS to keep two copies. When one stops, ECS can start a replacement from the task definition. Fargate supplies the computing capacity for those tasks so you do not manage the underlying EC2 hosts. It still needs a valid image, network settings and task resources. Use a one-off task for a short batch job and a service for an application that should keep accepting requests.
During a deployment, compare the desired count with the running count and check the task revision. Two running tasks show that the count was met. Checking the revision and making a request show whether the running tasks are the intended application.
| Word | Meaning | Kitchen |
|---|---|---|
| Cluster | A home for your services | One restaurant brand |
| Task definition | Recipe: image, CPU, memory, ports, env | Recipe card |
| Task | One running copy of that recipe | One order being cooked |
| Service | Keep N tasks running; replace unhealthy | Always 4 chefs on shift |
| Launch type | Fargate (no EC2) or EC2 (you manage) | Rented kitchen vs own |
3. Fargate vs EC2 launch type
Wrong launch type wastes money or ops time.
| Need | Prefer | Why |
|---|---|---|
| Student lab, spiky API, little ops time | Fargate | No AMI, no ASG, pay per task vCPU/memory |
| Steady 24×7 fleet, GPU, special kernel | EC2 launch type | Cheaper at huge steady load; more control |
| Same image on laptop and cloud | Either — image is the same | Portability comes from Docker |
Ravindra Bagale's Tip
Start labs on Fargate. Move to EC2 launch type only when a cost spreadsheet says so.
4. ECS vs Lambda vs EKS
- Lambda Event Functions on demand: event-driven invocations up to 30 minutes. They are designed for bounded invocations rather than a long-running application server.
- ECS: minutes-to-days containers, custom processes, sidecars, familiar Docker.
- EKS: full Kubernetes API when you already live in k8s or need multi-cloud k8s skills.
5. Pricing shape (mental model)
Fargate bills for vCPU and memory while the task runs. Idle service with desired count 0 costs almost nothing. ALB, NAT, ECR storage and data transfer are extra. Always set desired count to 0 or delete the service after a lab.
Common errors and fixes
These are synthetic practice situations. Diagnose the signal before changing a resource; no infrastructure changes are needed to read this guide.
A manually stopped task immediately comes back.
If a service still desires that task, its scheduler launches a replacement. Stopping one task is not the same as changing the service desired state. In a diagnosis, first identify whether this is a service-managed task or a standalone run. Repeatedly stopping replacements hides the real owner of the workload and can create needless churn.
Image pulling works but application calls to S3 fail.
The task execution role serves the agent, including supported image-pull and log actions. The task role supplies the application AWS permissions. A working image pull does not prove that the application has access to a bucket. Read the specific denial and task-role configuration rather than embedding access keys in the container image.
Scaling the service loses an uploaded local file.
A replacement task is a new compute instance of the recipe, not a continuation of another task local disk. Fargate ephemeral storage is tied to task lifetime. Put business data in an appropriate durable store and let stateless replicas refer to it. Increasing desired count does not copy local uploads across tasks.
Practical FAQ: decisions during real work
Is a running task the same as a healthy user experience?
No. RUNNING is a task lifecycle observation. A process can be running while returning application errors or failing a configured health check. Evaluate the relevant container or load-balancer health and application response separately. Define health around useful behavior, not just around a process existing.
Does registering a new task definition change every running service?
A registered revision is a new recipe, not proof that existing tasks were replaced. Inspect which revision the service deployment actually uses and which tasks are running. A release plan needs a deliberate service deployment and verification; merely saving a newer definition is not that deployment.
Can the cluster name act as the application endpoint?
A cluster is an organizational and scheduling boundary, not automatically a stable public application URL. Tasks can be replaced and their addresses can change. Use a deliberately configured load balancer or discovery mechanism for the intended traffic. Networking, routing and application identity remain design work even when hosts are managed.
6. Interview angles
Q. What is a task definition?
JSON recipe: image, CPU, memory, ports, IAM roles, env.
Q. Service vs task?
Task is one run; service keeps N healthy tasks.
Q. Why Fargate?
No EC2 to patch; pay per running task.
Q. ECS vs EKS?
ECS is AWS-native and simpler; EKS is Kubernetes-compatible.
Q. Where do images live?
Usually Amazon ECR in the same region.
Try at home
- Draw cluster → service → tasks → containers on paper.
- Read the next chapter and deploy nginx on Fargate.
In short
- ECS runs Docker containers on AWS.
- Fargate = no EC2 management for those tasks.
- Task definition is the recipe; service keeps N running.
- Clean up labs — Fargate bills while tasks run.
Decide who needs which AWS permissions
For a container that reads an S3 object, give that bucket access to the task role. Let the task execution role pull the ECR image and send logs. Then check that log delivery still works when the task role's S3 permission is missing.
Interview check Who uses the execution role?
The ECS/Fargate agent uses it for supported control-plane operations. Code inside the container obtains task-role credentials.
Practise this chapter
Interview questions and answers
When would you choose a one-off ECS task instead of a service?
I would use one task for a job with a definite end, such as generating a report once. I would use a service for an HTTP application that needs to stay available, because ECS can maintain a desired count and launch replacements.
Check your understanding
Choose an answer, then compare your reasoning with the explanation.
Predict a task replacement and inspect an ECS service
Before you start
- Use the sample below or an instructor-provided read-only ECS service. No new AWS service is needed.
Write a task definition note for one nginx container that listens on port 80. Give it revision 1 and set the service’s desired count to 2. List the separate names for image, task definition, two tasks and the service.
Expected result: The image is the packaged program, the definition lists settings, and the service uses revision 1 to keep two running task copies.
Read this supplied state: desired 2, running 1, pending 1. The stopped task used revision 1. Explain what you expect after the replacement starts.
Expected result: The service should return toward two running tasks. A replacement can have a new task ID while still using revision 1.
If you have an approved service, read its Tasks and Events tabs. Otherwise compare the fixture after deployment: two running tasks, both revision 2. Mark what you would test before claiming a new application is ready.
Expected result: Task count and revision confirm deployment state. A test request to the intended endpoint checks application behavior.
If the result differs
- If deployment stalls, check service events, task stopped reasons and health before changing desired count.
Finish and clean up
- No changes to reverse. If you later run the next chapter’s Fargate deployment lab, use its service and cluster cleanup steps.
Practice Tasks
Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.
About this scenarioMilan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 10
Easy Create an ECS cluster
A cluster is the logical home for your services and tasks.
Create an ECS cluster named milanfurniture-cluster.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy Create an ECR repository
ECR stores your Docker images privately inside AWS.
Create an ECR repository named milanfurniture/web.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Easy List running tasks
See which tasks are running in a cluster.
List the tasks in cluster milanfurniture-cluster.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Register a task definition
The task definition says which image, CPU, memory and ports a container uses.
Register the task definition in taskdef.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Pull images from one repository
CI or servers should pull only the images they run.
Allow ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on arn:aws:ecr:eu-south-1:126154182628:repository/milanfurniture/web, plus ecr:GetAuthorizationToken on *.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Scale a service
Change desired count to run more copies of the container.
Set service web in cluster milanfurniture-cluster to 4 tasks.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Trust policy for an ECS task role
The trust policy says WHO may take the role.
Write a trust policy that lets ecs-tasks.amazonaws.com assume the role.
- Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
- In each statement write Effect, Action and Resource.
- Give only what the task needs (least privilege).
- Use double quotes and commas correctly. Click Check answer.
Medium Redeploy a service
force-new-deployment restarts tasks so they pull the latest image.
Force a new deployment of service web in cluster milanfurniture-cluster.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Medium Get the ECR login password
Docker needs a short-lived password before it can push to ECR.
Get the ECR login password for eu-south-1.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Hard Create a Fargate service
A service keeps the desired number of tasks running and replaces failed ones.
In cluster milanfurniture-cluster, create service web using task definition milanfurniture-web:1, 2 tasks, launch type FARGATE, network config from net.json.
- Read the task and decide which AWS service it is about.
- Write aws, then the service name, then the operation (the action word).
- Add each needed flag (--name) followed by its value from the task.
- Flags can be in any order. Click Check answer.
Revision, comparison and interview cards
Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.







More cards: Ravi Sir's Cards.