Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

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.

One ECS Fargate request pathClient hitsALB or public IPECS service keepsN tasks healthyEach task runsyour container on FargateApp talks toRDS / DynamoDB / S3
You never SSH to patch the host OS on Fargate.

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.

WordMeaningKitchen
ClusterA home for your servicesOne restaurant brand
Task definitionRecipe: image, CPU, memory, ports, envRecipe card
TaskOne running copy of that recipeOne order being cooked
ServiceKeep N tasks running; replace unhealthyAlways 4 chefs on shift
Launch typeFargate (no EC2) or EC2 (you manage)Rented kitchen vs own

3. Fargate vs EC2 launch type

Wrong launch type wastes money or ops time.

NeedPreferWhy
Student lab, spiky API, little ops timeFargateNo AMI, no ASG, pay per task vCPU/memory
Steady 24×7 fleet, GPU, special kernelEC2 launch typeCheaper at huge steady load; more control
Same image on laptop and cloudEither — image is the samePortability 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

  1. Draw cluster → service → tasks → containers on paper.
  2. Read the next chapter and deploy nginx on Fargate.

In short

  1. ECS runs Docker containers on AWS.
  2. Fargate = no EC2 management for those tasks.
  3. Task definition is the recipe; service keeps N running.
  4. 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.

Reference documentation

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.

0 of 3 answered correctly
1. Which ECS object is one running copy of a task definition?

Answer and explanation

B. A task

A task is a running instance of the definition. The definition describes its images and settings.

2. Your web service should keep two copies running. Which ECS object maintains that desired count?

Answer and explanation

A. The ECS service

A service asks ECS to maintain the desired number of tasks and can replace a stopped task.

3. What does Fargate supply to ECS in this chapter?

Answer and explanation

C. Computing capacity without managing the underlying EC2 hosts

Fargate runs tasks on managed infrastructure, while images, permissions and networking stay part of your application setup.

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.
  1. 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.

  2. 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.

  3. 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 scenario

Milan 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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 *.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. 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.

Revision card: What is Amazon ECS?. Flipkart sale traffic needs many identical app boxes, not one fat EC2 that is hard to replace. Amazon ECS (Elastic Container Service) starts, stops and restarts Docker containers on AWS. Fargate is the launch type where you do not manage EC2 hosts. Define a task definition (image, CPU, memory, ports), put a service in a cluster, set desired count. A Fargate service with desired count 2 keeps two nginx tasks healthy behind a public IP or ALB.
Revision card: What is Amazon ECS? · Download PNG · Open card page
Revision card: Cluster, task, service. Wrong words in an interview sound like you never ran containers on AWS. Cluster = home for services. Task definition = recipe. Task = one running copy. Service = keep N tasks healthy. Create cluster → register task definition → create service with desired count and launch type Fargate. Kill one task; the service starts a replacement so count stays at N.
Revision card: Cluster, task, service · Download PNG · Open card page
ECS, Lambda, or EKS?. Use for bounded events. An on-demand Lambda Event Function supports up to 30 minutes per invocation. Use tasks or services when container processes and their lifecycle matter. Fargate can supply compute without managing EC2 hosts. Choose the Kubernetes API when you need its tooling and are ready for the operational work. Do not equate a container image with a Kubernetes requirement.
Revision card: ECS, Lambda, or EKS? · Download PNG · Open card page
Comparison: Fargate launch type vs EC2 launch type. Fargate launch type: AWS runs capacity; you pick vCPU/memory per task. EC2 launch type: You manage EC2/ASG/AMI for the cluster capacity.
Comparison: Fargate launch type vs EC2 launch type · Download PNG · Open card page
ECS vs Lambda. Runs container workloads as services or tasks. A service can maintain running tasks while a one-off task can run to completion. Runs bounded event-driven code. An on-demand Lambda Event Function can configure up to 30 minutes per invocation. Consider the trigger, runtime and state. Check the caller's timeout independently of the Lambda timeout.
Comparison: ECS vs Lambda · Download PNG · Open card page
Interview card: What is Fargate in ECS?. A launch type where AWS runs the capacity; you do not manage EC2 for those tasks. You still define CPU/memory in the task definition and pay while tasks run. Create service with Launch type Fargate; no ASG to patch for the lab.
Interview card: What is Fargate in ECS? · Download PNG · Open card page
Interview card: Task vs service in ECS?. A task is one running copy of a task definition. A service keeps N tasks healthy and replaces failures. Long-running APIs use a service; one-off batch may run a standalone task. desiredCount=2 on a service → two tasks; stop one → ECS starts another.
Interview card: Task vs service in ECS? · Download PNG · Open card page

More cards: Ravi Sir's Cards.