Ravindra BagaleCourses & study guides मराठी

Chapter 89: Amazon ECS for DevOps

DevOps

89.2 Cluster, task definition and service

Story. Meera in Pune explained ECS to a junior with three boxes on a whiteboard. The cluster is the parking lot. The task definition is the recipe: which image, CPU, memory, ports and roles. The service is the manager that keeps N copies of that recipe running and can register them with a load balancer. Once the junior saw recipe versus running copies, aws ecs create-service stopped looking magical.

Cluster holds capacity. Task definition is the recipe. Service keeps desired tasks running.
Cluster holds capacity. Task definition is the recipe. Service keeps desired tasks running.

Why

If you mix up task definition and service, you will update the wrong thing and wonder why traffic still hits the old image.

What

  • Cluster — Named place ECS schedules into (with Fargate capacity providers or EC2 instances).
  • Task definition — Versioned JSON/YAML family: container image, CPU, memory, port mappings, logs, roles.
  • Task — One running copy created from a task definition revision.
  • Service — Long-running controller: desired count, deployment strategy, load balancer hooks.
  • Revision — Each register-task-definition creates a new revision number (shop-api:3).

How

  1. Create a cluster (Console, CLI or Terraform).
  2. Register a task definition with your container image and resources.
  3. Create a service that points at that task definition and a desired count.
  4. Update the service when you want a new revision or a new count.

Example 1: Describe a service

aws ecs describe-services --cluster shop-prod --services pay-api --region ap-south-1 \
  --query 'services[0].{status:status,desired:desiredCount,running:runningCount,td:taskDefinition}' --output table

Output

----------------------------------------------------------------
|                       DescribeServices                       |
+---------+------------+----------+----------------------------+
|  desired|  running   |  status  |  td                        |
+---------+------------+----------+----------------------------+
|  2      |  2         |  ACTIVE  |  arn:...:task-definition/pay-api:7 |
+---------+------------+----------+----------------------------+

What it shows: The service wants 2 tasks, 2 are running, and they use task definition revision 7.

Example 2: Shape of a tiny Fargate task definition (excerpt)

{
  "family": "pay-api",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "pay-api",
      "image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/pay-api:1.4.0",
      "portMappings": [{ "containerPort": 8080, "protocol": "tcp" }]
    }
  ]
}

What it shows: Family name, Fargate compatibility, CPU/memory at task level, and one container image with port 8080. Account id and tag are examples.

Ravindra Bagale's Tip

Task definition = recipe. Service = keep N copies healthy. Cluster = where those copies run.

Quick quiz

Q1. What does a task definition hold?

Image, CPU, memory, ports, roles and log settings for containers.

Q2. What does an ECS service do?

Keeps a desired number of tasks running and can attach a load balancer.

Q3. What is a revision?

A new version number of a task definition family after you register again.

Did you understand? Let us try it once more.