Sub topic 6
Task execution role versus task role
This chapter assumes you know IAM roles and policies, have written a Fargate task definition, and can read JSON. A task definition can name two IAM roles. They look alike, but they act at different times and for different callers.
The two roles
| Row | Task execution role | Task role |
|---|---|---|
| Task definition field | `executionRoleArn` | `taskRoleArn` |
| Used by | ECS and Fargate agent | Your application code |
| When | Before and around container start | While the app runs |
| Typical jobs | Pull from ECR, write logs, fetch secrets | Read S3, write to DynamoDB, publish to SQS |
The execution role is the platform's identity. The task role is your application's identity. Your code never uses the execution role, and the agent never uses the task role.
Trust policy
Both roles must trust the ECS tasks service. Use the same trust policy for each:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ecs-tasks.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}Only the permissions attached to each role differ.
Execution role permissions
Attach the AWS managed policy AmazonECSTaskExecutionRolePolicy. It allows pulling images from ECR and writing to CloudWatch Logs. If the task definition uses secrets, the execution role also needs permission to read them:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/db-AbCdEf"
}
]
}The DB_PASSWORD secret in the earlier task definition is resolved by the agent. That is why this permission belongs on the execution role, not the task role.
Task role permissions
Grant only what your code calls. For example, a service that reads one bucket needs:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::my-app-data/*"
}
]
}The AWS SDK finds these credentials automatically through the container credentials endpoint. You do not pass keys.
Common errors
CannotPullContainerErrororResourceInitializationErrorat start. The execution role is missing, lacks ECR permissions, or cannot reach ECR over the network.- No logs appear. The execution role lacks
logs:CreateLogStreamandlogs:PutLogEvents, or the log group does not exist. AccessDeniedfrom your app at runtime. The permission was added to the execution role. Move it to the task role.- Secret fails to resolve. The execution role lacks
secretsmanager:GetSecretValue(orkms:Decryptfor a custom KMS key).
Exercise
A Fargate task starts, but your code gets AccessDenied when calling dynamodb:PutItem. Which role do you edit, and why?
Show answer
Edit the task role. DynamoDB calls come from your application code, which uses the task role. The execution role is used only by ECS and Fargate to pull images, fetch secrets, and write logs.
Exercise
A task fails to start because it cannot read a Secrets Manager secret listed in secrets. Which role needs the permission?
Show answer
The task execution role needs secretsmanager:GetSecretValue (and kms:Decrypt if the secret uses a customer managed key), because the agent resolves secrets before the container starts.