Core Concepts: Cluster, Task, Service

The four objects you use every day: clusters, task definitions, tasks, and services.

ECS has a small vocabulary. Learn these four words and the rest falls into place.

  • Cluster — a logical grouping where your tasks run.
  • Task definition — a blueprint (JSON) describing containers, CPU, memory, and ports.
  • Task — a running instance of a task definition.
  • Service — keeps a desired number of tasks running and replaces failed ones.
ECS cluster contains services, which run tasks, which run containersECS ClusterService: webTask 1container: appsidecar: logTask 2container: appService: workerTask 1container: job
A cluster holds services; each service keeps a desired number of tasks running; each task runs one or more containers.

A task definition is like a class; a task is an object created from it; a service is the supervisor that keeps the right number of objects alive.

Example

Example · bash
# The core objects, top to bottom:
aws ecs list-clusters
aws ecs list-services --cluster my-cluster
aws ecs list-tasks --cluster my-cluster

When to use it

  • A backend team registers a task definition once and lets ECS services keep exactly three API replicas alive across availability zones.
  • A QA engineer runs a one-off ECS task from a task definition to execute integration tests in the same container image used in production.
  • A platform team groups all production workloads into one ECS cluster per environment so IAM policies and CloudWatch dashboards stay isolated.

More examples

Register a Simple Task Definition

Registers a minimal Fargate task definition with one nginx container, awsvpc networking, and 0.25 vCPU / 512 MB memory.

Example · bash
aws ecs register-task-definition \
  --family web-api \
  --network-mode awsvpc \
  --requires-compatibilities FARGATE \
  --cpu 256 --memory 512 \
  --container-definitions '[{"name":"api","image":"nginx:alpine","portMappings":[{"containerPort":80}]}]'

Create an ECS Service

Creates a service that keeps two web-api tasks running in private subnets across two AZs using Fargate.

Example · bash
aws ecs create-service \
  --cluster prod-cluster \
  --service-name web-api-svc \
  --task-definition web-api:1 \
  --desired-count 2 \
  --launch-type FARGATE \
  --network-configuration \
    'awsvpcConfiguration={subnets=[subnet-a,subnet-b],securityGroups=[sg-web],assignPublicIp=DISABLED}'

Run a One-Off Task

Runs a single database-migration task outside of any service; the task exits when the migration script completes.

Example · bash
aws ecs run-task \
  --cluster prod-cluster \
  --task-definition db-migrate:5 \
  --launch-type FARGATE \
  --network-configuration \
    'awsvpcConfiguration={subnets=[subnet-a],securityGroups=[sg-migrate],assignPublicIp=DISABLED}' \
  --count 1

Discussion

  • Be the first to comment on this lesson.
Core Concepts: Cluster, Task, Service — Amazon ECS | SoundsCode