Controlling Access to Artifact Repositories
Deployment targets, build projects and other accounts all need access to artifacts, but only to the artifacts they use and only with the permissions they need. This lesson explains how IAM identity policies and resource-based policies combine to grant access to CodeArtifact, Amazon ECR and Amazon S3 repositories, including the cross-account and service-principal patterns that appear in deployment scenarios.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how identity-based and resource-based policies combine for same-account and cross-account access.
- Describe the permissions required to read from and publish to CodeArtifact repositories, including cross-account access.
- Explain how ECS, EKS, Lambda and other accounts are granted permission to pull images from Amazon ECR.
- Describe how CodeDeploy agents and other deployment targets are granted access to artifacts in Amazon S3.
Identity Policies and Resource Policies
Access to an artifact repository is decided by two kinds of policy. An identity-based policy is attached to the IAM role or user that makes the request and lists what that principal may do. A resource-based policy is attached to the repository, bucket or domain and lists which principals may access it. Within one account, either policy can grant access, provided nothing denies it. Across accounts, both are required: the resource policy in the owning account must allow the external principal, and the identity policy in the caller's account must allow the action on the resource. This rule explains most cross-account access failures.
When the repository is encrypted with a customer managed KMS key, a third policy is involved: the key policy must also allow the caller, or allow the account so that IAM policies can grant use of the key.
CodeArtifact Permissions
CodeArtifact has two resource policies. A domain policy controls actions on the domain, including which accounts may obtain authorisation tokens for it and create repositories in it. A repository policy controls who can read from and publish to a repository. The permissions required for common tasks are summarised below.
| Task | Identity policy actions | Resource policy needed across accounts |
|---|---|---|
| Obtain a token | codeartifact:GetAuthorizationToken on the domain, sts:GetServiceBearerToken | Domain policy allowing GetAuthorizationToken |
| Install packages | codeartifact:GetRepositoryEndpoint, codeartifact:ReadFromRepository | Repository policy allowing ReadFromRepository |
| Publish packages | codeartifact:PublishPackageVersion, codeartifact:PutPackageMetadata | Repository policy allowing publishing |
| Promote versions | codeartifact:CopyPackageVersions on source and destination | Policies on both repositories |
A typical multi-account design places the domain in a shared services account. The domain policy allows the organisation, using the aws:PrincipalOrgID condition key, to obtain tokens, and the repository policies allow read access to the whole organisation but publishing only to the build roles of the owning team. A condition such as sts:AWSServiceName equal to codeartifact.amazonaws.com on sts:GetServiceBearerToken limits bearer tokens to CodeArtifact.
Amazon ECR Permissions
Pulling an image requires ecr:GetAuthorizationToken, which applies to the registry and must be granted on all resources, plus ecr:BatchGetImage and ecr:GetDownloadUrlForLayer on the repository. Pushing additionally needs ecr:InitiateLayerUpload, ecr:UploadLayerPart, ecr:CompleteLayerUpload, ecr:BatchCheckLayerAvailability and ecr:PutImage. The identity that needs these permissions differs by service, and confusing them is a common cause of failed deployments.
- Amazon ECS: the task execution role pulls images and fetches secrets on behalf of the ECS agent. The managed policy
AmazonECSTaskExecutionRolePolicyincludes the ECR pull permissions. The task role is for the application's own AWS calls. - Amazon EKS: the node IAM role pulls images for pods on EC2 nodes (
AmazonEC2ContainerRegistryReadOnlyor the newer pull-only policy), and the Fargate pod execution role pulls images for Fargate pods. - AWS Lambda: for a container image function in the same account, the repository policy must allow the Lambda service principal; Lambda adds this when the function is created by a principal with permission to set the repository policy. For a cross-account repository, the repository policy must allow the function's account and the Lambda service principal.
- Another account: the repository policy allows the other account's role or account root, and the role's identity policy allows the pull actions. Using
aws:PrincipalOrgIDgrants pull access to every account in the organisation without listing them.
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "OrgPull",
"Effect": "Allow",
"Principal": "*",
"Action": ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer", "ecr:BatchCheckLayerAvailability"],
"Condition": {"StringEquals": {"aws:PrincipalOrgID": "o-a1b2c3d4e5"}}
}]
}This repository policy allows any principal in the organisation to pull images; each principal still needs an identity policy allowing the pull actions and ecr:GetAuthorizationToken. If the repository uses a customer managed KMS key, the key policy must also allow the pulling principals to decrypt.
The following diagram shows which identity pulls images for each service.

Amazon S3 Artifact Permissions for Deployment Targets
The CodeDeploy agent on an EC2 instance downloads the revision bundle from S3 using the instance's instance profile role, not the CodeDeploy service role. The instance profile therefore needs s3:GetObject (and s3:GetObjectVersion) on the artifact bucket and kms:Decrypt if the bucket uses a customer managed key. The CodeDeploy service role is used by CodeDeploy itself for actions such as reading Auto Scaling groups, tagging instances and managing load balancer registration. On-premises servers use an IAM user's access keys or, preferably, IAM Roles Anywhere or an IAM session-based registration.
Similar separation applies elsewhere. Elastic Beanstalk instances use their instance profile to read application versions; Systems Manager Run Command scripts that download artifacts use the instance profile; and CloudFormation reads templates from S3 with the caller's or the service role's credentials. VPC endpoint policies on an S3 gateway endpoint can further restrict which buckets instances may reach.
Instance profile, not service role
A CodeDeploy deployment that fails with an S3 access error during the DownloadBundle event is fixed by granting the instance profile access to the bucket. Adding S3 permissions to the CodeDeploy service role has no effect, because the agent uses the instance's credentials.
Who downloads the artifact?
Organisation-Wide Image Distribution
A platform team builds hardened base images in a shared services account. Application teams in 60 accounts must be able to pull them, accounts outside the organisation must not, and new accounts should gain access automatically.
The repository policy uses aws:PrincipalOrgID to allow the pull actions to the organisation, so new accounts gain access as soon as they join. The repository uses a customer managed KMS key whose key policy allows kms:Decrypt for principals with the same organisation condition. In each application account, the ECS task execution roles and EKS node roles already include the ECR pull permissions through their managed policies. Optionally, cross-account replication to each Region reduces cross-Region data transfer.
An ECS task in account B fails with CannotPullContainerError for an image in account A. The repository policy in account A allows account B. What else should be checked?
Show answer
The task execution role in account B must allow ecr:GetAuthorizationToken and the pull actions on the repository ARN, and if the repository uses a customer managed KMS key, the key policy must allow account B to decrypt. Network access to ECR (NAT gateway or VPC endpoints) should also be verified.
Exam Tips
Remember for the exam
- Cross-account access needs both the resource policy in the owning account and the identity policy in the caller's account; customer managed KMS keys add a key policy requirement.
- ECS image pulls use the task execution role; EKS uses the node role or Fargate pod execution role; Lambda needs the repository policy to allow the Lambda service principal.
- The CodeDeploy agent downloads revisions with the EC2 instance profile, not the CodeDeploy service role.
- Use aws:PrincipalOrgID in repository and domain policies to grant access to every account in an organisation.
- ecr:GetAuthorizationToken must be granted on all resources (*).