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

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.

What you'll learn 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

Why do we need this?

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.

What is it?

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

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

Why do we need this?

Wrong launch type wastes money or ops time.

What is it?

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: seconds of work, event-driven, 15 min max, no long processes.
  • 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.

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.

Practice Tasks

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

Why: A cluster is the logical home for your services and tasks.

What to do: Create an ECS cluster named milanfurniture-cluster.

How:
  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

Why: ECR stores your Docker images privately inside AWS.

What to do: Create an ECR repository named milanfurniture/web.

How:
  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

Why: See which tasks are running in a cluster.

What to do: List the tasks in cluster milanfurniture-cluster.

How:
  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

Why: The task definition says which image, CPU, memory and ports a container uses.

What to do: Register the task definition in taskdef.json.

How:
  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

Why: CI or servers should pull only the images they run.

What to do: Allow ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on arn:aws:ecr:eu-south-1:126154182628:repository/milanfurniture/web, plus ecr:GetAuthorizationToken on *.

How:
  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

Why: Change desired count to run more copies of the container.

What to do: Set service web in cluster milanfurniture-cluster to 4 tasks.

How:
  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

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets ecs-tasks.amazonaws.com assume the role.

How:
  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

Why: force-new-deployment restarts tasks so they pull the latest image.

What to do: Force a new deployment of service web in cluster milanfurniture-cluster.

How:
  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

Why: Docker needs a short-lived password before it can push to ECR.

What to do: Get the ECR login password for eu-south-1.

How:
  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

Why: A service keeps the desired number of tasks running and replaces failed ones.

What to do: In cluster milanfurniture-cluster, create service web using task definition milanfurniture-web:1, 2 tasks, launch type FARGATE, network config from net.json.

How:
  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
Revision card: ECS vs Lambda vs EKS. Picking Kubernetes when you only need a Docker web API wastes weeks of ops. Lambda: short event work (≤15 min). ECS: long-running containers and familiar Docker. EKS: full Kubernetes API when you already live in k8s. Start student labs on ECS Fargate. Move to EKS only when the job needs kubectl/Helm. A Node API that must stay warm for hours belongs on ECS, not on a 15-minute Lambda.
Revision card: ECS vs Lambda vs 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
Comparison: Amazon ECS vs AWS Lambda. Amazon ECS: Long-running containers, custom processes, sidecars. AWS Lambda: Short event-driven invokes, 15 min max.
Comparison: Amazon ECS vs AWS 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.