Sub topic 7
Amazon ECR repositories
This chapter assumes you can build and run a Docker image, use the command line, and have an AWS account with the AWS CLI configured. Authentication gets its own chapter, so here you will only create and configure a repository.
What an ECR repository is
Amazon Elastic Container Registry (ECR) is AWS's managed container registry. Every AWS account has a private registry in each region. The registry host name contains both values:
123456789012.dkr.ecr.us-east-1.amazonaws.com
Here 123456789012 is the account ID and us-east-1 is the region. You create one repository per image name, such as my-app, and each repository holds many tagged versions of that image.
A repository in one region is not visible from another. If you deploy in two regions, you either push to both or turn on replication.
Creating a repository
You must create a repository before you can push to it. ECR does not create one on first push.
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1The two options on this command are settings you should choose on purpose:
--image-tag-mutability IMMUTABLErejects any push that reuses an existing tag. Version1.0.0then always means the same image. The default isMUTABLE, which lets a tag move to a new image.scanOnPush=truescans each image for known vulnerabilities as soon as it is pushed. You read the findings in the console or with the CLI.
With immutable tags, you cannot keep a moving tag such as latest. Tag releases with a version number or commit SHA instead.
Expiring old images
Every stored image costs money, and CI can push hundreds of images. A lifecycle policy deletes images that match a rule. This one removes untagged images 14 days after they were pushed:
{
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images older than 14 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 14
},
"action": {
"type": "expire"
}
}
]
}Save it as policy.json and attach it to the repository:
aws ecr put-lifecycle-policy \
--repository-name my-app \
--lifecycle-policy-text file://policy.json \
--region us-east-1ECR evaluates rules in rulePriority order, lowest number first. Deleted images are gone, so check a rule against the images it matches before you rely on it. The console has a preview for this.
Common mistakes
- Pushing before creating the repository. The push fails with a "repository does not exist" error.
- Using the wrong region in the host name or in
--region. The repository exists, but not where you are looking. - Setting immutable tags and then re-pushing
latestin CI. The second push is rejected.
Exercise
You set a repository to IMMUTABLE and your CI job fails on its second run with the tag 1.0.0. What is the cause, and what is the fix?
Show answer
Immutable tags refuse a push to a tag that already exists, so the second run cannot overwrite 1.0.0. Give each build a unique tag, such as the commit SHA or a new version number, instead of reusing one.
Exercise
Which two details in an ECR registry host name identify where the repository lives?
Show answer
The AWS account ID and the region, as in 123456789012.dkr.ecr.us-east-1.amazonaws.com.