ECS tasks, services and definitions
From an image to a running service
Amazon Elastic Container Service (ECS) schedules containers. An ECR image is the packaged application; a task definition describes how to run it; a task is one running or stopped instance of that definition. A service maintains a desired number of tasks and replaces failed tasks. A cluster is a logical grouping where tasks and services are scheduled. ECS is the orchestrator; Fargate supplies compute without requiring you to manage the underlying servers.
A small bookstore API
Imagine a bookstore API listening on port 8080. Register definition bookstore:1, then configure a service with desired count 2. ECS attempts to maintain two tasks, each running the API. If one stops, the service tries to replace it, subject to capacity, permissions and network availability. A standalone task is suitable for a finite report job; it does not acquire the service replacement behavior automatically. A task may contain several containers, such as an API and a telemetry sidecar, but the service counts tasks, not individual containers.
Read a task definition
The static JSON below illustrates a Linux/x86-64 Fargate task in eu-west-1. CPU 256 means 0.25 vCPU; memory 512 is MiB. The digest is fictional and cannot be deployed. The execution role ARN is also fictional. user requests a numeric nonroot identity; the image must support it. essential means stopping this container stops the task. The definition does not create a service, VPC, log group or security group. No commands are executed.
{ "family": "bookstore", "requiresCompatibilities": [ "FARGATE" ], "networkMode": "awsvpc", "cpu": "256", "memory": "512", "runtimePlatform": { "operatingSystemFamily": "LINUX", "cpuArchitecture": "X86_64" }, "executionRoleArn": "arn:aws:iam::111122223333:role/example-ecs-execution", "containerDefinitions": [ { "name": "api", "image": "111122223333.dkr.ecr.eu-west-1.amazonaws.com/bookstore/api@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "essential": true, "user": "10001", "portMappings": [ { "containerPort": 8080, "protocol": "tcp" } ] } ]}Versions and desired state
Registering another definition creates a new revision, for example bookstore:2; it does not by itself update a running service. A deployment is the process of moving the service to its intended configuration. During deployment the running count may differ from the desired count. Keep the approved image digest and task revision together in release records. A running task is not necessarily ready to handle requests.
Practice and next step
Choose whether a nightly report and a continuously available API need a standalone task or a service. Explain why two tasks do not guarantee two Availability Zones. Next, select compute capacity and estimate operating cost. Review the earlier ECR and VPC lessons if image references or subnet terminology are unfamiliar.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ECS tasks, services and definitions — knowledge check
Apply the lesson to offline ECS scenarios.
Which ECS object maintains the desired count for the bookstore API?
A report should execute once and then exit. What fits?
Two tasks each contain an API and a telemetry sidecar. How many containers are expected?
Register bookstore:2 while the service still references bookstore:1. What happens immediately to the service?
What does CPU 256 mean in the example?