Skip to content

Amazon ECR security: best practices for container images

The registry is where the container supply chain lives or dies. See how to protect images in ECR.

Reading time
10 min
Published

Amazon ECR stores the container images that run in your environments, which makes it a central point of the supply chain. All it takes is one image with known vulnerabilities, a mutable tag that quietly starts pointing to something else or a repository with overly broad permissions to compromise everything that runs from there. That is why ECR security revolves around the provenance, integrity and freshness of images.

ECR security best practices have one goal: every image promoted to production should be trustworthy and traceable. To get there, you need to take care of vulnerability scanning, tag immutability, the image lifecycle and push and pull permissions. Signing and a gate in the pipeline round out the supply chain safeguards.

Vulnerability scanning and CVEs

Turn on ECR image scanning to identify CVEs in the packages of each image. Scan on push catches problems early, and continuous scanning re-evaluates stored images as new vulnerabilities are published. This matters because an image that was clean a few months ago can pile up CVEs without any change to your code. Without continuous scanning, you end up running those vulnerabilities in production without knowing it.

Basic scanning and enhanced scanning

ECR has two scanning modes. Basic scanning uses a database of operating system CVEs and runs on push. Enhanced scanning is done by Amazon Inspector: it also covers programming language dependencies, re-evaluates images continuously as new CVEs appear and sends findings to Security Hub. In production, enhanced scanning with continuous re-evaluation usually pays for itself, because it is what notices the image aging.

How to turn on scan on push and tag immutability for the repository
aws ecr put-image-scanning-configuration \  --repository-name my-app \  --image-scanning-configuration scanOnPush=trueaws ecr put-image-tag-mutability \  --repository-name my-app \  --image-tag-mutability IMMUTABLE

Decide in advance what to do with the findings: block promotion of images with critical vulnerabilities, prioritize the fixes and update the base image. Without a handling process, the scan turns into a report nobody reads, and it reduces no risk at all.

Immutable tags and lifecycle

Mutable tags are a trap. The same tag can point to different images over time, so the image you tested is not necessarily the one that is running. With tag immutability enabled, a published tag always refers to the same image. For reproducible builds, reference images by digest.

Lifecycle policies clean up old and unused images. That reduces cost and lowers the chance of someone deploying an obsolete, vulnerable version. Keep what you need for rollback and expire the rest. The rule below, for example, expires untagged images after 14 days, since they tend to pile up in the repository.

Lifecycle policy that expires untagged images
{  "rules": [    {      "rulePriority": 1,      "description": "Expire untagged images after 14 days",      "selection": {        "tagStatus": "untagged",        "countType": "sinceImagePushed",        "countUnit": "days",        "countNumber": 14      },      "action": { "type": "expire" }    }  ]}
  • Enable scan on push and continuous enhanced scanning
  • Act on findings instead of just producing reports
  • Turn on tag immutability
  • Reference production images by digest
  • Create a lifecycle policy that expires old images

Permissions and the supply chain

Use repository and IAM policies, with least privilege, to restrict who pushes and who pulls images. Take extra care with public repositories: they expose images to anyone and can leak whatever sensitive content is baked into them. Also control the provenance of base images, prefer trusted sources and rebuild your images from time to time to inherit fixes from the base.

The repository policy defines who can pull the image. Allowing pulls only from the deploy role of the right workload, instead of leaving them open to the whole account, reduces the reach of a compromised credential and keeps any identity in the organization from pulling images it should not.

Repository policy that allows pulls only from a specific role
{  "Version": "2012-10-17",  "Statement": [    {      "Sid": "AllowPullOnlyFromDeployRole",      "Effect": "Allow",      "Principal": {        "AWS": "arn:aws:iam::ACCOUNT:role/deploy-my-app"      },      "Action": [        "ecr:GetDownloadUrlForLayer",        "ecr:BatchGetImage"      ]    }  ]}

Provenance: signing and verifying images

Scanning and immutability tell you the image has few CVEs and that the tag does not change, but they do not prove who built the image. Signing closes that gap. The pipeline signs each promoted image, and the runtime environment only accepts images with a valid signature from a trusted key. This way, an image injected from outside the pipeline is rejected at deployment, even if it carries the expected tag.

In practice, this combines a signing tool (AWS Signer with Notation, or a sigstore signer) with a check at deploy time. In Kubernetes, an admission controller validates the signature before letting the pod start. Outside Kubernetes, verification becomes a step of the deploy itself. From then on, trust stops depending on the tag and starts depending on the signature.

A gate in the pipeline: from build to deploy

Image governance only reduces risk for real when it becomes a barrier on the way to production. Make the checks mandatory pipeline steps, because in practice nobody reads an optional report.

  • Run the scan and fail the build if there is a critical CVE without an approved exception
  • Sign the approved image and promote it by digest, never by a tag that can change
  • Block any image without a valid signature at deploy time
  • Record who approved each vulnerability exception and until when it is valid
  • Rebuild images periodically to inherit fixes from the base image

There will be exceptions, because not every critical CVE has an immediate fix. In a healthy process, an exception is a conscious decision, with an owner and a deadline. What must not happen is someone quietly turning off the block the first time it gets in the way.

Practical checklist

  • Enable scan on push and continuous enhanced scanning through Inspector
  • Define how CVE findings will be handled
  • Prevent promotion of images with critical vulnerabilities
  • Enable tag immutability on repositories
  • Point production deployments at the image digest
  • Expire old images with a lifecycle policy
  • Limit push and pull with repository and IAM policies
  • Keep internal images out of public repositories
  • Sign promoted images and verify the signature at deploy time
  • Record each vulnerability exception with an owner and a deadline
  • Check the provenance of base images
  • Rebuild images from time to time to inherit fixes

Best practices

  • Remember that everything that runs passes through the registry, a critical point in the supply chain
  • Use continuous enhanced scanning, because new CVEs appear after the build
  • Use tag immutability so what was tested is what runs
  • Apply least privilege to push and pull
  • Sign images and reject any without a valid signature at deploy time
  • Control your base images and rebuild to inherit fixes
  • Expire old images, which only bring risk and cost

Common mistakes

Mutable tags in production
The same tag starts pointing to another image, and what was tested is no longer what runs.
Trusting the tag alone, without verifying signatures
An image injected outside the pipeline, with the expected tag, goes live with no barrier at all.
Pull open to the whole account
Any compromised identity can pull internal images it should not be able to reach.
Scan on push only, with no continuous re-evaluation
Images pile up CVEs discovered after the build, and nobody finds out.
Public repository holding an internal image
Sensitive content baked into the image is visible to anyone.
Ignored scan findings
Known vulnerabilities reach production without being addressed.
No lifecycle policy configured
Obsolete, vulnerable images remain available for deployment.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

What is Amazon ECR and what role does it play in container security?
Amazon ECR (Elastic Container Registry) is the AWS container image registry. It is where images are stored and from where they are distributed to services such as ECS and EKS. That makes ECR a critical point in the container supply chain, because a compromised or vulnerable image published there spreads to every environment that consumes it. Vulnerability scanning, immutable tags and restricted permissions in ECR are the controls that help you ship only trusted images to production.
Why enable continuous scanning if I already scan on push?
Because new vulnerabilities are discovered all the time. An image approved on push can pile up CVEs weeks later, without any change to your code. Continuous scanning re-evaluates stored images against the latest CVEs, so you find out when an image running in production has become vulnerable after the build.
What are immutable tags and why do they matter?
With immutable tags, a published tag always refers to the same image. Without them, the same tag can point to different images over time, and the image you tested may not be the one running in production. Immutability makes deployments predictable and is the foundation for reproducible, traceable builds.
Should I block deployment of images with critical CVEs?
Yes. It is good practice to connect the scan result to the pipeline and block promotion of images with known critical vulnerabilities, or at least require a conscious approval. Scanning only has value when there is a handling process, because a report nobody acts on does not reduce risk. The block forces the fix to happen before production.
Is a public repository in ECR safe?
For distributing open images, public repositories have a legitimate use. The risk lies in internal images, which may contain embedded configuration or secrets. For that content, use private repositories with restricted permissions. And before making any image public, check that no layer carries sensitive data.
What is the difference between basic and enhanced scanning in ECR?
Basic scanning checks the image's operating system CVEs and runs on push. Enhanced scanning is done by Amazon Inspector, also covers programming language dependencies, re-evaluates images continuously as new CVEs emerge and sends findings to Security Hub. For production images, enhanced scanning with continuous re-evaluation usually pays off, because it is what detects when the image becomes vulnerable after the build.
Why sign images if I already scan and use immutable tags?
Scanning measures vulnerabilities and an immutable tag keeps the tag from changing, but neither proves who built the image. Signing settles the question of provenance. The runtime environment accepts only images signed by a trusted key, and an image injected from outside the pipeline is rejected at deploy time even if it carries the expected tag. Trust then depends on where the image came from, and its name is no longer enough.
How do I reference an image by digest instead of by tag?
The digest is the hash of the image content, in sha256 format, and it identifies exactly that artifact. Instead of pointing the deployment at repository:app:production, point it at repository:app@sha256:HASH. That way, what was tested and approved is literally what runs, and you do not depend on a tag that, in a repository without immutability, could start pointing to another image.

When to bring in specialist help

Bringing scanning, immutability and image governance into the pipeline is part of GUARDIASEC's Hardening service. We assess the container supply chain and deliver the recommended controls in order of priority, based on risk.