Push artifacts to AWS ECR repository
Push container images to an ECR repository with AWS SSO or IAM user credentials.
This guide pushes a container image and an OCI Helm chart to Amazon ECR from your workstation or a pipeline, using AWS SSO or IAM user credentials, and installs the chart back from the registry.
The steps below create the repositories with the AWS CLI. If you manage repositories with the Repository API instead, create them as described in Create a Repository and skip the aws ecr create-repository commands.
Prerequisites
Section titled “Prerequisites”There is no Infralib flag for pushing to ECR. If you create the repositories with the Repository API, the prerequisites of that guide apply.
- AWS CLI, Docker and, for charts, Helm.
- An AWS SSO user role or IAM user with this policy attached:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "ECRCreateRepository", "Effect": "Allow", "Action": "ecr:CreateRepository", "Resource": "*" }, { "Sid": "ECRRepositoryAccess", "Effect": "Allow", "Action": [ "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability", "ecr:CompleteLayerUpload", "ecr:GetDownloadUrlForLayer", "ecr:InitiateLayerUpload", "ecr:PutImage", "ecr:UploadLayerPart", "ecr:ListImages", "ecr:DescribeImages", "ecr:DescribeRepositories" ], "Resource": "arn:aws:ecr:<region>:<account_id>:repository/*" }, { "Sid": "ECRAuthToken", "Effect": "Allow", "Action": "ecr:GetAuthorizationToken", "Resource": "*" } ]}This policy gives push, pull and create-repository rights for all ECR repositories in the specified account and region. Narrow it as needed, for example drop ECRCreateRepository if repositories are created with the Repository API.
1. Set up user credentials
Section titled “1. Set up user credentials”export AWS_REGION=<region> # e.g eu-north-1export AWS_ACCESS_KEY_ID="AKI....."export AWS_SECRET_ACCESS_KEY="UO/gR....."For an SSO user session, also set:
export AWS_SESSION_TOKEN="IQoJb3....."2. Log in to ECR
Section titled “2. Log in to ECR”aws ecr get-login-password | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com3. Tag and push a Docker image
Section titled “3. Tag and push a Docker image”Create the ECR repository:
aws ecr create-repository --repository-name testimageTag the Docker image and push it to ECR:
docker tag some_local_image:tag <account_id>.dkr.ecr.<region>.amazonaws.com/testimage:tagdocker push <account_id>.dkr.ecr.<region>.amazonaws.com/testimage:tag4. Create and push an OCI Helm chart
Section titled “4. Create and push an OCI Helm chart”aws ecr create-repository --repository-name testcharthelm create testcharthelm package testcharthelm push testchart-0.1.0.tgz oci://<account_id>.dkr.ecr.<region>.amazonaws.com5. Verify
Section titled “5. Verify”List the pushed image:
aws ecr describe-images --repository-name testimageInstall the Helm chart from the repository:
helm install testchart oci://<account_id>.dkr.ecr.<region>.amazonaws.com/testchart --version 0.1.0Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause / fix |
|---|---|
| Push of an existing tag is rejected | The repository uses IMMUTABLE tags (for repositories created with the Repository API, the cluster-wide imageTagMutability). Push a unique tag per build, see EnvironmentConfig. |
| An image pushed earlier is gone | A lifecycle rule removed it. Repositories created with the Repository API get the cluster-wide lifecycleRules, see EnvironmentConfig. |
AccessDenied on push, pull or create-repository |
The role or user is missing a permission from the policy in Prerequisites, or the repository is outside the region and account in its Resource. |
EnvironmentConfig
Section titled “EnvironmentConfig”For repositories created with the Repository API, two cluster-wide settings can affect what happens when you push:
| Setting | Default | What it does |
|---|---|---|
imageTagMutability |
MUTABLE |
If set to IMMUTABLE, pushing a tag that already exists is rejected. Every build must use a unique tag. |
lifecycleRules |
[] (no policy) |
Automatic clean-up rules. An image you pushed earlier may already have been removed. |
Both are described in more detail in Create a Repository.