Sub topic 3
Controlling access with IAM
This chapter assumes you know what IAM users, roles, and policies are, and that you have the AWS CLI configured. It covers how IAM decides who can push, pull, and manage images in Amazon ECR. The next chapter uses these permissions to log Docker in.
How ECR checks permissions
ECR uses two kinds of policy, and a request can be allowed by either one in some cases.
- Identity-based policies are attached to an IAM user, group, or role. They say what that principal may do.
- Resource-based policies are attached to a repository. They say who may act on that repository. They are called repository policies and are covered in a later chapter.
Within a single account, a request succeeds if any policy allows it and no policy explicitly denies it. Across accounts, both sides must agree. The calling account's principal needs an identity policy that allows the action, and the repository policy in the owning account must allow that principal.
Two parts of ECR make its permissions different from most services. First, authentication is a separate API call, and it cannot be limited to one repository. Second, pushing and pulling are not single actions. Each one is a sequence of several API calls, and your policy must allow all of them.
The authorization token permission
Before Docker can talk to a registry, the caller asks ECR for a temporary token. That call is ecr:GetAuthorizationToken. It applies to the whole registry, not to a repository, so it must be granted with "Resource": "*".
{
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
}Without this statement, aws ecr get-login-password fails with an AccessDeniedException, no matter what else the policy allows. This is the most common mistake when writing a custom ECR policy.
Pull permissions
A docker pull reads a manifest, then downloads each layer. Three actions cover it, and they are scoped to a repository ARN.
| Action | What it allows |
|---|---|
| `ecr:BatchGetImage` | Read image manifests |
| `ecr:GetDownloadUrlForLayer` | Get a URL to download each layer |
| `ecr:BatchCheckLayerAvailability` | Check whether layers exist in the repository |
BatchCheckLayerAvailability is not strictly needed for a plain pull in every client, but AWS includes it in its managed read-only policies, and some tools call it. Include it unless you have a reason not to.
Here is a policy that lets a principal pull from one repository only:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetToken",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PullMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
"ecr:BatchCheckLayerAvailability"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}The repository ARN has the form arn:aws:ecr:<region>:<account-id>:repository/<name>. If your repositories use namespaces such as team-a/api, you can use a wildcard like repository/team-a/* to cover a whole group.
Push permissions
A push uploads layers in parts and then writes the manifest. It needs every pull action above, plus these:
| Action | What it allows |
|---|---|
| `ecr:InitiateLayerUpload` | Start uploading a layer |
| `ecr:UploadLayerPart` | Upload one chunk of a layer |
| `ecr:CompleteLayerUpload` | Finish a layer upload |
| `ecr:PutImage` | Write the manifest and tag |
A push-capable policy for one repository looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetToken",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PushPullMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}Leaving out one upload action causes a push to fail partway. Docker may upload some layers and then print denied or no basic auth credentials, which can be confusing. When a push fails after some progress, compare your policy with the list above.
Management permissions
Creating and configuring repositories uses separate actions. Keep them away from CI pipelines that only push images.
ecr:CreateRepositoryandecr:DeleteRepositorymanage repositories.CreateRepositoryis scoped by repository ARN, so you can limit it to a name prefix.ecr:DescribeRepositories,ecr:DescribeImages, andecr:ListImagesread metadata. Thedescribe-imagescommand from the push and pull chapter needsecr:DescribeImages.ecr:BatchDeleteImageremoves images.ecr:PutLifecyclePolicy,ecr:SetRepositoryPolicy, andecr:PutImageTagMutabilitychange repository settings.ecr:StartImageScanandecr:DescribeImageScanFindingsrun and read scans.
Splitting these into separate roles is a good habit. A build pipeline gets push access. A platform team gets management access. Runtime workloads get pull access only.
Using AWS managed policies
AWS publishes managed policies that bundle common sets of actions. They are a quick start, but they usually grant access to all repositories ("Resource": "*").
| Managed policy | Typical use |
|---|---|
| `AmazonEC2ContainerRegistryReadOnly` | Pull from any repository in the account |
| `AmazonEC2ContainerRegistryPowerUser` | Push and pull, but no repository deletion or policy changes |
| `AmazonEC2ContainerRegistryFullAccess` | Everything, including deletion |
Use these for experiments. For production, copy the relevant statements into a customer-managed policy and narrow the Resource to the repositories each role needs.
Restricting by repository name and tags
Scoping by ARN is the main least-privilege tool. Two other techniques help.
You can use a name prefix to give each team its own area:
{
"Effect": "Allow",
"Action": [
"ecr:CreateRepository",
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:BatchCheckLayerAvailability"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/team-a/*"
}You can also use condition keys. For example, ecr:ResourceTag/<key> matches tags on a repository, and aws:RequestTag/<key> checks tags supplied on a create request. These let you enforce that repositories are tagged with an owner, or that a role only touches repositories with a matching tag.
Roles beat long-lived keys
The principal that calls ECR should be an IAM role wherever possible.
- A CI system should assume a role, ideally through OIDC federation, instead of storing an access key.
- An ECS task or EKS pod pulls with a role (covered in the final chapter), not with credentials baked into the image.
- A developer laptop should use IAM Identity Center or an assumed role.
Roles issue short-lived credentials, so a leaked value expires. You can confirm which identity your CLI is using before debugging a permission error:
aws sts get-caller-identityDiagnosing access errors
The sequence below shows where each permission is checked when you push.
When something fails, work through these checks.
- Run
aws sts get-caller-identityto confirm the account and role. - Check that the Region and account ID in the registry URL match the repository ARN in your policy.
- Confirm the policy includes
ecr:GetAuthorizationTokenon*. - Compare the allowed actions with the pull or push list above.
- Look for an explicit
Denyin a permissions boundary, a service control policy, or a repository policy. An explicit deny always wins. - For cross-account access, check both the identity policy and the repository policy.
Exercise
A build role has a policy that allows ecr:BatchCheckLayerAvailability, ecr:InitiateLayerUpload, ecr:UploadLayerPart, ecr:CompleteLayerUpload, and ecr:PutImage on repository/my-app. docker push still fails. Name two actions that are missing and explain why each is needed.
Show answer
ecr:GetAuthorizationToken is missing. It must be allowed on Resource: "*", because without it the role cannot get a token to log Docker in. ecr:BatchGetImage (and normally ecr:GetDownloadUrlForLayer) is also missing. These are part of the standard push-and-pull set that AWS grants together, and they are needed when the client reads existing manifests or layers. The token permission is the one that blocks the push before any layer is sent.
Exercise
You want a CI role to push images only to repositories whose names start with payments/, and you want a runtime role to only pull. Which resources and actions would you give each role?
Show answer
Both roles need ecr:GetAuthorizationToken on *. The CI role gets the pull actions plus InitiateLayerUpload, UploadLayerPart, CompleteLayerUpload, and PutImage on arn:aws:ecr:<region>:<account-id>:repository/payments/*. The runtime role gets only BatchGetImage, GetDownloadUrlForLayer, and BatchCheckLayerAvailability on the specific repositories it deploys from.