Lesson 1 of 6 Amazon ECS, EKS and Fargate

Running Containers with Amazon ECS

This lesson explains Amazon ECS, AWS's own container orchestrator, which runs tasks from task definitions and keeps services at their desired count, with deep integration into other AWS services.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe task definitions, tasks, services and clusters.
  • Distinguish the task execution role from the task role.
  • Describe how ECS handles deployments, logs and secrets.

Building blocks

Amazon ECS is built on four concepts. A task definition is the blueprint for an application: the container images, CPU and memory, ports, environment variables and secrets, logging configuration and IAM roles. A task is a running copy of a task definition; a task run on its own, such as a batch job, stops when its containers exit.

A service keeps a desired number of tasks running for a long-running application. It replaces tasks that fail, registers new tasks with a load balancer, deploys new versions gradually and can scale the number of tasks automatically. A cluster is the logical group in which tasks and services run, on EC2 instances or Fargate capacity. The following table summarises the concepts.

ConceptWhat it is
Task definitionThe blueprint: image, CPU, memory, ports, environment, secrets, logging and IAM roles
TaskA running copy of a task definition
ServiceKeeps a desired number of tasks running, replaces failures and registers tasks with a load balancer
ClusterThe logical group in which tasks run, on EC2 or Fargate capacity

The following diagram shows how the four concepts relate.

The ECS building blocks. A task definition is the blueprint: images, CPU and memory, ports, secrets, logging and IAM roles. A service keeps a desired number of tasks, running copies of the task definition, replaces failed tasks and registers them with a load balancer. Tasks run in a cluster on EC2 or Fargate capacity.
The desired count is illustrative. The service, not the cluster, keeps the tasks running and registered.
Match the pairs

ECS concepts

Exam skills 2.1.4

Match each ECS concept with what it is.

Log in to try this activity Interactive activities are free with an account.

ECS IAM roles

An ECS task can use two IAM roles, which serve different parties. The task execution role is used by the ECS agent, or Fargate, to prepare and support the task: pulling images from ECR, sending logs to CloudWatch Logs and fetching the secrets that the task definition injects. A task that fails to start because it cannot pull its image or read a secret needs a change to this role.

The task role is used by the application inside the containers: its credentials are available to the AWS SDK, so the code's calls to services such as S3 or DynamoDB use its permissions. An application that receives AccessDenied needs a change to the task role. Different tasks on the same host can have different task roles, which keeps each application to least privilege. The following table summarises the two roles.

RoleUsed byFor
Task execution roleThe ECS agentPulling ECR images, writing logs, fetching secrets
Task roleYour applicationIts AWS API calls

Confusing the two is a common error.

The following diagram shows which party uses each role.

The two IAM roles of an ECS task. The task execution role is used by the ECS agent or Fargate to pull images from ECR, send logs to CloudWatch Logs and fetch the secrets the task definition injects. The task role is used by the application code for its own AWS API calls, such as reading from S3.
The symptom points to the role: a start-up failure to the execution role, an AccessDenied from the application to the task role.
Sort into groups

Task role or execution role?

Exam skills 1.1.2 · 2.1.4

Decide which role each permission belongs to.

Log in to try this activity Interactive activities are free with an account.

Deployments, logs and secrets

Amazon ECS replaces tasks gradually during a rolling update, and the deployment circuit breaker rolls a failing deployment back automatically when new tasks cannot reach a healthy state. Native ECS blue/green deployments start the new version as a separate set of tasks and shift traffic to it, allowing instant rollback.

Container logs are sent to CloudWatch Logs with the awslogs log driver, or to other destinations with FireLens. The task definition's secrets field injects values from Secrets Manager or Parameter Store into containers as environment variables. Container images are stored in Amazon ECR, which scans them for vulnerabilities, removes old images with lifecycle policies, can make tags immutable and replicates images across Regions.

True or false

ECS deployments

Exam skills 2.2.1 · 2.1.4

Check how ECS deploys, logs and stores images.

Log in to try this activity Interactive activities are free with an account.
Worked example

A task that will not start

ECS task start-up failures are usually caused by missing permissions in one of the task's roles. The following table identifies the role at fault and the remedy for several symptoms.

SymptomRole at faultFix
CannotPullContainerError from ECRTask execution roleAttach AmazonECSTaskExecutionRolePolicy; check the network path to ECR
Secrets cannot be injected at start-upTask execution roleAllow secretsmanager:GetSecretValue on the secret
The application gets AccessDenied writing to S3Task roleGrant s3:PutObject to the task role

The execution role is for ECS itself; the task role is for the application. With the deployment circuit breaker enabled, a release whose tasks cannot start is rolled back automatically.

Check yourself

What is the difference between an ECS task definition and a service?

Show answer

A task definition describes the containers to run. A service keeps a chosen number of tasks running, replaces failed ones and can register them with a load balancer.

Exam Tips

Remember for the exam

  • To give each container task its own AWS permissions, use the ECS task role.
  • If a task cannot pull its image or write logs, check the task execution role.
  • If the application in a container gets AccessDenied calling S3, check the task role.
  • To roll back a failing ECS deployment automatically, use the deployment circuit breaker.
  • To inject database credentials into containers, use task definition secrets from Secrets Manager.