Sub topic 3
Controlling access with IAM
This chapter assumes you know what IAM users, roles, and policies are, and that you have an ECR repository such as my-app from the previous chapter. It covers which IAM actions Docker operations need and how to write least-privilege policies for them.
How ECR authorizes a request
Every ECR API call is checked against IAM. Two kinds of policy take part:
- An identity-based policy is attached to a user or role. It says what that identity may do.
- A repository policy is attached to a repository. It says who may use that repository. The later chapter on repository policies covers it.
Within one account, an Allow in either policy is enough, as long as nothing denies the request. For cross-account access, both sides must allow it: the repository policy in the owning account and an identity policy in the caller's account.
Two scopes of permission
ecr:GetAuthorizationToken applies to the registry as a whole, not to a repository. You cannot limit it to one repository, so its statement must use "Resource": "*". Every other action targets a repository ARN, such as arn:aws:ecr:us-east-1:123456789012:repository/my-app. You can scope those actions to one repository or to a prefix such as repository/team-a/*.
A push policy
Pushing needs the token action plus the layer upload actions and PutImage, which writes the manifest.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetToken",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PushToMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}A push role usually also needs pull access, because Docker checks which layers already exist before it uploads them.
A pull-only policy
A deployment role that only runs images needs less:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetToken",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PullFromMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}Giving CI a push-only role and giving runtime a pull-only role limits the damage if either credential leaks. Listing images with aws ecr describe-images needs ecr:DescribeImages, which neither policy above includes.
AWS managed policies
AWS provides ready-made policies, including AmazonEC2ContainerRegistryReadOnly and AmazonEC2ContainerRegistryPowerUser. They grant access to every repository in the account. They are convenient for experiments. For production, write policies scoped to specific repositories.
Common errors
| Symptom | Likely cause |
|---|---|
| `no basic auth credentials` on push or pull | The login step was skipped, or the 12-hour token expired. |
| `not authorized to perform: ecr:GetAuthorizationToken` | The identity policy lacks the token statement, or the statement is scoped to a repository ARN instead of `*`. |
| `denied: User ... is not authorized to perform: ecr:InitiateLayerUpload` | The login worked, but the push actions are missing or the Resource ARN has the wrong Region, account, or repository name. |
| Cross-account pull denied | Only one side allows it. Both the repository policy and the caller's identity policy must. |
| `not authorized ... ecr:DescribeImages` | Listing needs its own action, separate from push and pull. |
An explicit Deny in any applicable policy, including a service control policy or permissions boundary, overrides every Allow. Check these when an Allow seems to be ignored.
Exercise
A CI role has a policy with only the PushToMyApp statement shown above, without the GetToken statement. docker login fails. Which statement is missing, and why can't its Resource be the repository ARN?
Show answer
The role needs a statement allowing ecr:GetAuthorizationToken. That action applies to the registry rather than to any one repository, so it must use "Resource": "*". A repository ARN would never match the request.