Skip to content
Docs

Push artifacts to AWS ECR repository

Push container images to an ECR repository with AWS SSO or IAM user credentials.

Updated
On this page

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.

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.

Terminal window
export AWS_REGION=<region> # e.g eu-north-1
export AWS_ACCESS_KEY_ID="AKI....."
export AWS_SECRET_ACCESS_KEY="UO/gR....."

For an SSO user session, also set:

Terminal window
export AWS_SESSION_TOKEN="IQoJb3....."
Terminal window
aws ecr get-login-password | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com

Create the ECR repository:

Terminal window
aws ecr create-repository --repository-name testimage

Tag the Docker image and push it to ECR:

Terminal window
docker tag some_local_image:tag <account_id>.dkr.ecr.<region>.amazonaws.com/testimage:tag
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/testimage:tag
Terminal window
aws ecr create-repository --repository-name testchart
helm create testchart
helm package testchart
helm push testchart-0.1.0.tgz oci://<account_id>.dkr.ecr.<region>.amazonaws.com

List the pushed image:

Terminal window
aws ecr describe-images --repository-name testimage

Install the Helm chart from the repository:

Terminal window
helm install testchart oci://<account_id>.dkr.ecr.<region>.amazonaws.com/testchart --version 0.1.0
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.

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.