Sub topic 8
Repository policies and sharing across accounts
This chapter assumes you can write basic IAM policies, know the difference between an identity-based policy and a resource-based policy, and have already created a repository and pushed an image. It covers how an ECR repository policy works, how it combines with IAM to allow cross-account access, and the errors you are most likely to hit.
Two kinds of policy, one decision
Access to an ECR repository is controlled by two policy types:
- Identity-based policies are IAM policies attached to a user, group, or role. They say what that identity may do.
- Repository policies are resource-based policies attached to one repository. They say who may use that repository.
Which policies must allow a request depends on whether the caller is in the same account as the repository.
| Scenario | What must allow the request |
|---|---|
| Caller and repository in the same account | Either the IAM policy or the repository policy is enough |
| Caller in a different account | Both the repository policy in the owning account and an IAM policy in the caller's account |
In both cases, an explicit Deny in any applicable policy overrides every Allow.
Anatomy of a repository policy
A repository policy is a JSON document with the same structure as other IAM resource policies. Each statement has an effect, a principal, and actions. You do not specify a Resource, because the policy is attached to the repository it protects.
The following policy lets account 111122223333 pull images from the repository:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCrossAccountPull",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111122223333:root"
},
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
}
]
}Apply it from the account that owns the repository:
aws ecr set-repository-policy \
--repository-name my-app \
--policy-text file://policy.jsonThree details matter here:
- The two actions are the ones Docker needs to pull.
ecr:BatchGetImagefetches the image manifest, andecr:GetDownloadUrlForLayerprovides a download URL for each layer. - The principal
arn:aws:iam::111122223333:rootmeans "the account", not the root user. It delegates the decision to that account's own IAM administrators. Nobody in account111122223333can use the repository until they also hold a matching IAM permission. set-repository-policyreplaces the whole policy. To add a statement, fetch the current policy withaws ecr get-repository-policy, edit it, and apply the complete document.
The caller's side: IAM in the consuming account
The repository policy only opens the door. A role in account 111122223333 also needs an identity-based policy. Registry login is a separate call that is not scoped to a repository, so ecr:GetAuthorizationToken must be allowed on Resource: "*".
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}Note that the repository ARN in the Resource field contains the owning account ID (123456789012), not the caller's account.
When the caller logs in to Docker, it must use the registry of the account that owns the images:
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comThen it pulls as usual, preferably by digest:
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>Choosing the principal
The Principal element controls how widely you share. Narrower is safer.
| Principal | Effect | Use when |
|---|---|---|
| `arn:aws:iam::ACCOUNT:root` | Any identity in that account that its IAM policies permit | You trust the other account's administrators |
| `arn:aws:iam::ACCOUNT:role/NAME` | Only that role | You want to name the exact consumer |
| `arn:aws:iam::ACCOUNT:user/NAME` | Only that user | Rare. Prefer roles |
| A service principal such as `lambda.amazonaws.com` | That AWS service, for services that pull on your behalf | The service documents it requires this |
| `"*"` | Anyone, subject to conditions | Almost never. See the warning below |
Sharing with an AWS Organization
Listing every account is tedious when you have many consumers. You can instead allow all accounts in your organization with a condition key:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowOrgPull",
"Effect": "Allow",
"Principal": "*",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
],
"Condition": {
"StringEquals": {
"aws:PrincipalOrgID": "o-exampleorgid"
}
}
}
]
}New accounts that join the organization gain access automatically, as long as their IAM policies also permit it. That is convenient, and it also means you should review who can create accounts in the organization.
Granting push access
Pulling needs only read actions. Pushing needs more, because Docker uploads layers in parts and then registers the manifest. A push-capable statement typically adds these actions:
ecr:BatchCheckLayerAvailabilityecr:InitiateLayerUploadecr:UploadLayerPartecr:CompleteLayerUploadecr:PutImage
ecr:BatchCheckLayerAvailability is also used during pulls by some clients, so you will often see it in pull-only policies as well. If a pull fails with a denied action you did not expect, check the error message for the exact action name.
Prefer to grant push access to a CI role in the owning account and let other accounts only pull. A single writer makes it easier to audit who published an image and to keep tag immutability meaningful.
Restricting access further with conditions
Repository policies support IAM condition keys. Two are useful with ECR:
aws:PrincipalOrgIDlimits access to an organization, as shown above.aws:SourceVpceoraws:SourceVpclimits access to requests that arrive through a specific VPC endpoint or VPC. This only works for requests that actually pass through an endpoint.
A common pattern is an explicit deny for requests that come from outside the expected network:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyPullOutsideVpce",
"Effect": "Deny",
"Principal": "*",
"Action": [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
],
"Condition": {
"StringNotEquals": {
"aws:SourceVpce": "vpce-0123456789abcdef0"
}
}
}
]
}Be careful with this. Image layers are served from Amazon S3, so a private network design also needs an S3 gateway endpoint, and the endpoint policy must allow the ECR layer bucket. A deny like the one above can also lock out your own administrators and your CI system when they operate from outside the VPC. Test it against every consumer before you apply it to a production repository.
Verifying and debugging access
Check what is currently attached:
aws ecr get-repository-policy --repository-name my-app --region us-east-1To test a consumer, assume the consumer role in the other account and run docker pull. A policy that looks right in the console can still fail for the reasons below.
| Symptom | Likely cause |
|---|---|
| `no basic auth credentials` | Docker is not logged in to the owning account's registry, or the token expired. Tokens last 12 hours |
| `denied: User ... is not authorized to perform: ecr:GetAuthorizationToken` | The caller's IAM policy lacks `ecr:GetAuthorizationToken` on `*` |
| `denied` on `ecr:BatchGetImage` from another account | The repository policy is missing, or names the wrong principal, or the caller's IAM policy does not allow the action |
| `Repository does not exist` | Wrong account ID or Region in the image URI. A repository is Regional |
| Works for one role, not another | The repository policy names a specific role ARN. Roles that were deleted and recreated get a new internal ID, so check the principal still resolves |
| Policy rejected on save | A statement has an invalid principal, for example an account ID that is not 12 digits |
Checklist for sharing a repository
- In the owning account, write a repository policy that names the narrowest principal that works and only the actions needed.
- Apply it with
aws ecr set-repository-policy. - In the consuming account, attach an IAM policy that allows
ecr:GetAuthorizationTokenon*and the pull actions on the repository ARN. - Log in to the owning account's registry hostname.
- Pull by digest, so the consumer deploys the exact content you reviewed.
- Re-check with
get-repository-policyafter any change, sinceset-repository-policyoverwrites the previous document.
Exercise
Account 111122223333 can run docker login against account 123456789012's registry, but docker pull fails with a denied error on ecr:BatchGetImage. The repository policy in 123456789012 allows arn:aws:iam::111122223333:root to use ecr:BatchGetImage and ecr:GetDownloadUrlForLayer. What is the most likely missing piece, and where do you fix it?
Show answer
The caller's identity-based IAM policy in account 111122223333 does not allow ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on the repository ARN (arn:aws:ecr:us-east-1:123456789012:repository/my-app). Cross-account access needs an allow in both the repository policy and the caller's IAM policy. The repository policy with the :root principal only delegates the decision to the other account, so the fix goes in the consuming account's IAM role or user policy.
Exercise
You run aws ecr set-repository-policy with a file that contains only a new statement allowing another team's role to pull. Afterwards, the original consumer reports access errors. Why, and how do you avoid it?
Show answer
set-repository-policy replaces the entire existing policy, so the earlier statements were removed. Run aws ecr get-repository-policy first, add the new statement to the existing document, and apply the full combined policy.