Sub topic 2
ECS building blocks: clusters, tasks, and services
This chapter assumes you can build a Docker image, read JSON, use the AWS CLI, and know what IAM roles, subnets, and security groups are. Here you learn the four ECS objects that every later chapter builds on.
The four objects
- Cluster: a logical grouping of tasks and services. It holds no servers of its own when you use Fargate. It is a namespace for your workloads, and it is where you attach capacity providers and monitoring settings.
- Task definition: a versioned blueprint. It lists the containers, image, CPU, memory, ports, and roles. Each change creates a new numbered revision, such as
web-app:3. A revision is never edited in place. - Task: one running instance of a task definition. A task is a group of one or more containers that start and stop together and share a network interface.
- Service: a controller that keeps a desired number of copies of a task running. If a task stops, the service starts a replacement.
Two ways to run a task
ECS gives you two ways to start tasks, and they suit different workloads.
| Approach | Behavior when the task exits | Typical use |
|---|---|---|
| `run-task` (standalone task) | Nothing restarts it | Batch jobs, migrations, one-off scripts |
| Service | Replaces it to maintain the desired count | Web apps, APIs, workers |
A standalone task is a one-shot request. A service is a control loop. It compares the desired count with the running count and starts or stops tasks until they match. This is why services are the right choice for anything long-lived.
Working with the objects
Create a cluster, then inspect what it contains:
aws ecs create-cluster --cluster-name demo
aws ecs list-task-definitions --family-prefix web-app
aws ecs describe-services \
--cluster demo \
--services web \
--query "services[0].{desired:desiredCount,running:runningCount,pending:pendingCount,taskDef:taskDefinition}"If desired and running differ for long, the service is failing to place or keep tasks. Later chapters cover the usual causes.
Revisions and families
A task definition has a family, which is its name. Each registration of the same family creates a new revision. If you reference only the family, as in --task-definition web-app, ECS uses the latest active revision. Production services usually pin a specific revision, for example web-app:3. A service update that points at a new revision is how you deploy.
Common mistakes
- Editing a task definition expecting running tasks to change. Running tasks keep the revision they started with. To change them, register a new revision and update the service.
- Using
run-taskfor a web server. Nothing restarts it after a crash. - Treating the cluster as a security boundary. Access is controlled by IAM and networking, not by cluster membership.
Exercise
A task started from revision 2 of web-app is running. You register revision 3. What happens to the running task, and what must you do to move a service to revision 3?
Show answer
The running task keeps using revision 2. Registering a revision does not change running tasks. You update the service to reference web-app:3 (or the family, which resolves to the latest revision), and the service then starts tasks from revision 3 and stops the old ones.