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.
| Concept | What it is |
|---|---|
| Task definition | The blueprint: image, CPU, memory, ports, environment, secrets, logging and IAM roles |
| Task | A running copy of a task definition |
| Service | Keeps a desired number of tasks running, replaces failures and registers tasks with a load balancer |
| Cluster | The logical group in which tasks run, on EC2 or Fargate capacity |
The following diagram shows how the four concepts relate.

ECS concepts
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.
| Role | Used by | For |
|---|---|---|
| Task execution role | The ECS agent | Pulling ECR images, writing logs, fetching secrets |
| Task role | Your application | Its AWS API calls |
Confusing the two is a common error.
The following diagram shows which party uses each role.

Task role or execution role?
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.
ECS deployments
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.
| Symptom | Role at fault | Fix |
|---|---|---|
CannotPullContainerError from ECR | Task execution role | Attach AmazonECSTaskExecutionRolePolicy; check the network path to ECR |
| Secrets cannot be injected at start-up | Task execution role | Allow secretsmanager:GetSecretValue on the secret |
| The application gets AccessDenied writing to S3 | Task role | Grant 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.
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.