Managing Container Images with Amazon ECR
Amazon Elastic Container Registry (Amazon ECR) stores, scans and distributes container images and other OCI artifacts for Amazon ECS, Amazon EKS, AWS Lambda and other runtimes. This lesson explains repository settings such as tag immutability and encryption, lifecycle policies, image scanning, replication, pull-through cache rules and image signing.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how tag mutability affects deployment traceability and rollback.
- Interpret ECR lifecycle policy rules and their evaluation order.
- Distinguish between basic scanning and enhanced scanning with Amazon Inspector.
- Describe cross-Region and cross-account replication, pull-through cache rules and image signing.
Registries, Repositories and Images
Each AWS account has one private registry per Region, addressed as <account>.dkr.ecr.<region>.amazonaws.com. The registry contains repositories, and each repository holds image versions. An image is identified by its immutable digest (a SHA-256 hash of its manifest) and optionally by one or more tags such as 1.8.2 or latest. Clients authenticate with a token from GetAuthorizationToken, valid for 12 hours, usually through aws ecr get-login-password piped into docker login. Amazon ECS, EKS and Lambda authenticate automatically using their roles.
Repositories can also store Helm charts and other OCI artifacts, such as signatures and software bills of materials (SBOMs). Amazon ECR Public is a separate public registry for sharing images publicly.
Tag Mutability
By default, tags are mutable: pushing a new image with an existing tag moves the tag to the new image. This makes tags such as latest convenient but dangerous for deployment. If a task definition references orders:latest, a scaling event or task replacement can start a newer image than the one that was tested, and rolling back to a previous task definition revision does not restore the previous code, because the tag now points to new content.
Setting a repository to immutable tags rejects any push that would overwrite an existing tag. Combined with unique tags, such as the commit ID or a semantic version, every deployment references exactly one image, and rollback is reliable. ECR also supports exclusion filters with immutability, so that a few tags, such as latest, can remain mutable for convenience while version tags are protected. For complete certainty, deployments can reference the image digest instead of a tag.
Mutable tags in exam scenarios
When a scenario says that rolling back to an earlier task definition still runs new code, or that a deployed image changed without a deployment, the cause is a mutable tag such as latest. The remedy is unique version tags with tag immutability enabled.
Encryption and Repository Creation Templates
ECR encrypts images at rest with Amazon S3-managed encryption (AES-256) by default, or with AWS KMS (SSE-KMS, optionally dual-layer DSSE-KMS) using an AWS managed or customer managed key. The encryption setting is chosen when the repository is created and cannot be changed later; changing it requires a new repository. Repository creation templates define settings, including encryption, tag mutability, lifecycle policy, repository policy and tags, that ECR applies to repositories it creates automatically through pull-through cache or replication, so that every repository meets organisational standards.
Lifecycle Policies
Without cleanup, repositories accumulate thousands of images, which increases storage cost and makes it hard to find the right image. A lifecycle policy is a set of rules that expire images automatically. Each rule has a priority, a selection and an action. The selection chooses images by tagStatus (tagged, untagged or any), optionally by tag prefixes or wildcard patterns, and by a count condition: imageCountMoreThan keeps the newest N images, and sinceImagePushed expires images older than N days. Newer options include conditions based on when an image was last pulled and moving images to the lower-cost archive storage class instead of deleting them.
Rules are evaluated in priority order, lowest number first, and each image is matched by at most one rule. A rule with tagStatus: any must have the highest priority number, so that it is evaluated last. Lifecycle policies can be tested with a preview before they are applied.
{
"rules": [
{"rulePriority": 1, "description": "Expire untagged images after 7 days",
"selection": {"tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 7},
"action": {"type": "expire"}},
{"rulePriority": 2, "description": "Keep the 50 most recent release images",
"selection": {"tagStatus": "tagged", "tagPatternList": ["v*"], "countType": "imageCountMoreThan", "countNumber": 50},
"action": {"type": "expire"}}
]
}Image Scanning
ECR offers two scanning types, configured at the registry level with optional repository filters.
Basic scanning
Basic scanning uses AWS-native technology to scan the operating system packages of an image for known vulnerabilities (CVEs). Scans run on push or manually, and each image can be scanned once a day. It is free.
Enhanced scanning
Enhanced scanning integrates with Amazon Inspector. It scans both operating system packages and programming language packages, such as npm, pip and Maven dependencies, and it supports continuous scanning: when a new CVE is published, Inspector rescans affected images without a new push, for a configurable duration after push or last pull. Findings appear in Inspector, in ECR and in AWS Security Hub, and they generate EventBridge events. It is charged per image scanned.
To block vulnerable images from being deployed, a pipeline can wait for the scan and fail on findings above a severity, for example with the Amazon Inspector scan action, or an EventBridge rule on Inspector findings can notify teams or start remediation.
Basic or enhanced scanning?
Replication and Pull-Through Cache
Replication is configured at the registry level. Rules copy images to other Regions (cross-Region replication) and to other accounts (cross-account replication), with optional repository name filters. Cross-account replication requires a registry permissions policy in the destination account that allows the source account to replicate. Only images pushed after replication is configured are replicated. Replication serves two purposes: lower latency and data transfer cost by pulling images from a registry in the same Region as the workload, and resilience, because workloads in a recovery Region do not depend on the primary Region's registry.
Pull-through cache rules let a private registry cache images from upstream registries such as Amazon ECR Public, Docker Hub, the Kubernetes container registry, Quay, GitHub Container Registry, Azure Container Registry, GitLab Container Registry and other ECR registries. Clients pull through a prefix, such as <account>.dkr.ecr.<region>.amazonaws.com/docker-hub/library/nginx:1.27; ECR fetches the image on first use, stores it in a private repository and refreshes it periodically. This avoids public registry rate limits, keeps traffic private and lets enhanced scanning cover third-party images. Credentials for authenticated upstreams are stored in Secrets Manager.
Run the registry
Image Signing
Signing proves that an image was produced by a trusted build and has not been modified. ECR supports signatures stored as OCI artifacts alongside the image. Images can be signed with AWS Signer and the Notation client, and ECR also offers managed signing, in which ECR signs images on push using a Signer signing profile. Verification happens at deployment: on Amazon EKS through an admission controller such as Kyverno or Ratify that rejects unsigned images, and on AWS Lambda through code signing configurations for container and ZIP packages.
Reliable and Clean Image Management
A team deploys to ECS in two Regions. It has 9,000 images in one repository, rollbacks have deployed the wrong code, and a security audit found that nobody is told when a new vulnerability affects a running image.
-
Traceable tags
Tag images with the commit ID in the buildspec and enable tag immutability, so each task definition revision references a unique image.
-
Cleanup
Add a lifecycle policy that expires untagged images after 7 days and keeps the newest 100 release images.
-
Continuous scanning
Enable enhanced scanning with continuous scanning and an EventBridge rule that sends critical findings to the team's SNS topic.
-
Second Region
Configure cross-Region replication so the second Region's tasks pull from a local registry.
A lifecycle policy has a rule with tagStatus any at priority 1 and a rule for untagged images at priority 2. Why is this policy rejected?
Show answer
A rule with tagStatus any must have the highest rulePriority value so it is evaluated last. Placing it first would leave nothing for later rules to match.
Exam Tips
Remember for the exam
- Enable tag immutability and use unique tags (commit ID or version) so rollbacks deploy the intended image; mutable tags such as latest break traceability.
- Lifecycle policies select by tagStatus and count (imageCountMoreThan or sinceImagePushed); tagStatus any must be the last rule.
- Enhanced scanning uses Amazon Inspector, covers OS and language packages and rescans continuously; basic scanning covers OS packages on push.
- Registry replication copies new images across Regions and accounts; cross-account needs a registry policy in the destination.
- Pull-through cache rules cache images from Docker Hub, ECR Public and other registries in a private repository.
- ECR repository encryption is chosen at creation and cannot be changed.