ECS task definitions and services

Intermediate · 15 min

Clusters, task definitions, tasks and services on ECS: the two IAM roles, secrets and logs, load balancing, deployments, rollback and service autoscaling.

Before thisECR image registryWorkload identity for EC2, Lambda and CIElastic Load Balancing

Tiny Technical Tutorials · 15 min

ECS task definitions and services

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Tiny Technical TutorialsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A task definition is a versioned description of containers. A task is one running copy of a revision. A service keeps a chosen number of tasks running.
  • The execution role lets ECS pull images, fetch secrets and write logs. The task role is what your own code uses to call AWS.
  • Secrets referenced from Secrets Manager or Parameter Store are injected at start. Rotating one does not change running tasks.
  • By default a rolling update keeps 100 percent of desired tasks healthy and may run up to 200 percent while it replaces them.
  • Turn on the deployment circuit breaker with rollback, and add CloudWatch alarms for failures that only show up in metrics.
  • ECS also does blue/green, linear and canary deployments natively, with bake time and lifecycle hooks.

Something has to keep it running

docker run on one machine works until the process dies at night, the machine fails, or you need six copies behind a load balancer and a new version without downtime. That is the orchestration problem. Amazon ECS is AWS’s own orchestrator: there’s no control plane for you to run, and it plugs directly into IAM, load balancers and CloudWatch. If you want the Kubernetes API instead, that is EKS.

Four words carry the model:

  • Cluster: a named place where tasks run, with its compute capacity.
  • Task definition: the recipe. Images, CPU, memory, ports, roles, logging.
  • Task: one running instance of a recipe, holding one or more containers.
  • Service: a controller that keeps N tasks of a recipe running.

The task definition

{
  "family": "shop-api",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "512", "memory": "1024",
  "executionRoleArn": "arn:aws:iam::123456789012:role/shop-api-execution",
  "taskRoleArn": "arn:aws:iam::123456789012:role/shop-api-task",
  "containerDefinitions": [{
    "name": "api",
    "image": "123456789012.dkr.ecr.eu-west-1.amazonaws.com/shop/api:1.4.0",
    "essential": true,
    "portMappings": [{"containerPort": 8080}],
    "secrets": [{"name": "DB_PASSWORD",
                 "valueFrom": "arn:aws:secretsmanager:...:secret:shop/db-AbCdEf"}],
    "logConfiguration": {"logDriver": "awslogs", "options": {
      "awslogs-group": "/ecs/shop-api", "awslogs-region": "eu-west-1",
      "awslogs-stream-prefix": "api"}}
  }]
}

Registering this creates shop-api:1. Registering again creates shop-api:2. Revisions don’t change, so rolling back means pointing the service at an earlier number. On moto, a local emulator, two registrations returned :1 and :2. moto also accepted "cpu": "300", which real Fargate rejects, so don’t treat an emulator’s success as validation.

Two roles, two jobs. The execution role is used by ECS to start the task: pull the image, read the referenced secrets, write logs. The task role is what your code gets when its SDK asks for credentials. Keep them separate and small.

If an essential container stops, the whole task stops. Sidecars such as log routers can share the task and its network. With awsvpc networking each task gets its own network interface and security group.

Injected secrets are read once, at start. After rotating one, force a new deployment.

The service

A service keeps the desired count running, spreads tasks across Availability Zones, and replaces any that stop or fail a health check. Attach an Application Load Balancer target group and tasks are registered and deregistered as they come and go. Services can find each other with Service Connect, which gives short names and per-call metrics. One-off and batch work runs as standalone tasks, without a service.

On moto, creating a service with --desired-count 3 showed desiredCount 3, runningCount 0. moto never starts containers, but the gap is the useful idea: desired state is what you declare, running count is what ECS has achieved, and a deployment problem shows up as the two refusing to meet. The service’s events list is the first place to look.

Deploying a new revision

The default is a rolling update, bounded by two percentages. minimumHealthyPercent (default 100 for a replica service) is the floor of healthy tasks; maximumPercent (default 200) is the ceiling of tasks during the change. With 4 desired, ECS can start 4 new ones, wait for health, then stop the old ones.

If the new version cannot start, a service would keep trying forever. The deployment circuit breaker counts failed tasks, marks the deployment failed and, with rollback enabled, returns to the last completed one. It catches crashes and failed health checks. It doesn’t know that your new version returns wrong prices, so add CloudWatch alarm based failure detection on error rate or latency.

ECS resolves image tags to digests during a deployment, so tasks started later run the same bytes even if the tag moved.

ECS also has blue/green, linear and canary strategies built in. A second set of tasks runs beside the first, traffic shifts at once or in steps, both stay up for a bake time for quick rollback, and lifecycle hooks can run tests or pause. The general patterns are in deployment strategies.

Scaling

Service auto scaling changes the desired count. Start with target tracking, for example average CPU at 60 percent or requests per target. This scales tasks only. Where those tasks find capacity is the next lesson.

For a simple web service, ECS Express Mode creates the service, load balancer, certificate and scaling from a few inputs.

About the video

The embed is Tiny Technical Tutorials’ ECS walkthrough, August 2022, 15 minutes. Its chapters cover the terms (cluster, service, task, task definition, launch types) in the first three minutes, then create a cluster, a task definition running NGINX and a service in the console, and clean up. The console has changed since, and it doesn’t cover the two roles in depth, circuit breakers or blue/green. I judged it from chapters and description only, since transcripts couldn’t be fetched. English was judged from the “English (US) original” audio track. A longer 2026 video on task definitions is in deeper.

What was run: registering revisions and creating the service ran on moto 5.1.22, which starts no containers. Deployment behaviour, roles, secrets and digest resolution are from the AWS docs, checked 2026-11-29.

Where this fits

The service says what should run. Next, what it runs on: Fargate and ECS capacity.

Check yourself

  1. 01Your task fails to start with an access denied error while pulling a secret. You add the permission to the task role and nothing changes. Why?

    Fetching secrets for injection is done by ECS before your code runs, using the execution role. The task role only covers calls made by the application itself.

  2. 02You push a new image under the same tag and expect running services to pick it up on the next restart of a crashed task. Do they?

    Not by default. ECS resolves the tag to a digest for a deployment and keeps using that digest, so every task in the service runs the same image. Force a new deployment, or better, register a revision with a new tag or digest.

  3. 03A service has desired count 4, minimumHealthyPercent 100 and maximumPercent 100. What happens on deploy?

    It is stuck. ECS may not stop an old task without dropping below 4 healthy, and may not start a new one without exceeding 4. One of the two limits needs room.

  4. 04New tasks start, then the load balancer marks them unhealthy before the app has finished booting. What setting helps?

    The service's health check grace period, which tells ECS to ignore failing health checks for that long after a task starts. Also check the health check path and the security group between load balancer and task.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark