Sub topic 7
Preventing tag overwrites with tag immutability
This chapter assumes you can build, tag, and push an image to a private ECR repository, and that you know how an IAM policy allows or denies an action. It also assumes you know the difference between a tag and a digest from the previous chapter.
Why tags need protection
A tag is a human-readable label that points at one image manifest. In a repository that allows tag changes, you can push a new image under a tag that already exists. ECR moves the tag to the new image. The old image stays in the repository, but it becomes untagged and nothing refers to it by name.
This is convenient for a tag like latest, and risky for anything else. Consider a deployment that references my-app:1.0.0:
- A teammate rebuilds from a different commit and pushes
1.0.0again. Every new container that starts from that tag now runs different code. - An incident review asks what was running last Tuesday. The tag
1.0.0no longer answers that question, because it now points somewhere else. - A vulnerability scan result for
1.0.0was clean. After the overwrite, that result describes an image the tag no longer names.
Tag immutability removes this failure mode at the registry. When it is on, ECR refuses any push that would move an existing tag.
The repository setting
Tag mutability is a setting on each repository, not on the registry as a whole. It has two main values:
| Value | Behavior |
|---|---|
| `MUTABLE` | A push with an existing tag moves the tag to the new image. This is the default. |
| `IMMUTABLE` | A push with an existing tag is rejected. Each tag can be assigned once. |
ECR also offers variants that let you exclude tags matching a filter, for example so that latest stays mutable while everything else is locked. The exact option names have changed over time, so check the current ECR documentation before relying on them. The rest of this chapter uses plain IMMUTABLE.
You set the value when you create the repository. This is the command from the earlier chapter on repositories:
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1You can also change it on an existing repository:
aws ecr put-image-tag-mutability \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--region us-east-1To check the current value:
aws ecr describe-repositories \
--repository-names my-app \
--region us-east-1 \
--query 'repositories[0].imageTagMutability' \
--output textChanging the setting affects only future pushes. Tags that were already moved around before you switched stay where they are. If you turn immutability on, review the repository first and confirm that each existing tag points at the image you intend.
What happens on a rejected push
With IMMUTABLE set, push 1.0.0 once and it succeeds. Rebuild with changed code, tag it 1.0.0 again, and push. The upload of layers may proceed, but the final step, writing the manifest under the tag, fails. Docker prints an error similar to this:
tag invalid: The image tag '1.0.0' already exists in the 'my-app' repository and cannot be overwritten because the repository is immutable.The underlying API error is ImageTagAlreadyExistsException. A CI job that sees this error should fail rather than retry, because retrying cannot succeed. The fix is a new tag.
The rule applies to every tag, including latest. In an immutable repository you push latest once, and the next push of latest fails. If your tooling pushes latest on every build, it will break the first time it runs against an immutable repository.
Choosing tags that work with immutability
Immutability pushes you toward tags that are unique per build. Common schemes:
- Semantic versions such as
1.4.2. Good for released artifacts. You must bump the version for every push. - Git commit SHAs such as
a3f9c1d82b47. Unique by construction and traceable to source. - Build numbers or timestamps such as
build-1842or20261126T175812Z. Unique, but less traceable on their own. - A combination, such as
1.4.2-a3f9c1d82b47. Readable and unique.
A small CI script can derive the tag from the commit and skip the push if the image already exists:
#!/usr/bin/env bash
set -euo pipefail
REGION=us-east-1
REGISTRY=123456789012.dkr.ecr.${REGION}.amazonaws.com
REPO=my-app
TAG=$(git rev-parse --short=12 HEAD)
if aws ecr describe-images \
--repository-name "$REPO" \
--image-ids imageTag="$TAG" \
--region "$REGION" >/dev/null 2>&1; then
echo "Tag $TAG already exists in $REPO, skipping push."
exit 0
fi
docker build -t "$REPO:$TAG" .
docker tag "$REPO:$TAG" "$REGISTRY/$REPO:$TAG"
docker push "$REGISTRY/$REPO:$TAG"describe-images returns an ImageNotFoundException and a non-zero exit code when the tag is absent, so the if falls through to the build. Any other failure, such as missing credentials, also falls through, and the push then fails with a clearer message.
If you want a moving pointer like "the current production image", keep that pointer outside the registry. Store the immutable tag or digest in a deployment config, a parameter, or a Git repository, and update that reference when you promote a build.
Immutability and digests
Immutability makes a tag stable, but it does not make a tag equal to a digest in every respect. Two situations remain:
- Deleting and re-pushing. Someone with
ecr:BatchDeleteImagecan delete the image under1.0.0, and then push a different image as1.0.0. The tag was never moved, because it was removed and created again. Lifecycle policies can also expire tagged images, which frees the tag name for reuse. - Switching the setting. Someone with
ecr:PutImageTagMutabilitycan set the repository back toMUTABLE, overwrite a tag, and set it back toIMMUTABLE.
A digest cannot be reassigned, because it is a hash of the manifest content. For deployments that must be exactly reproducible, reference the image by digest, for example my-app@sha256:<digest>. Use immutable tags as the readable layer and digests as the exact one.
Protecting the setting with IAM
Because the setting can be switched off, treat it as a control that needs its own permissions. Most principals that push images need only upload permissions, and do not need ecr:PutImageTagMutability or ecr:BatchDeleteImage. This identity policy denies both actions on the repository to anyone it is attached to:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ProtectTagImmutability",
"Effect": "Deny",
"Action": [
"ecr:PutImageTagMutability",
"ecr:BatchDeleteImage"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}Attach it to CI roles and developer roles, and keep these two actions for a small administrative role. Note that denying ecr:BatchDeleteImage also stops that role from deleting images manually. Lifecycle policy expiry is performed by the ECR service and is not blocked by this deny.
Interaction with other features
- Scanning. Findings attach to an image, which you find by digest or tag. With immutable tags, a clean scan result for
1.0.0stays tied to the same image for as long as that tag exists. - Lifecycle policies. Rules still expire images in an immutable repository. A rule that expires old tagged images can free a tag name, so choose tag schemes that never repeat, such as SHAs, so reuse cannot happen by accident.
- ECS and EKS. Both resolve a tag to a digest when they start a container. With a mutable tag, two tasks started at different times can run different images. With an immutable tag, they run the same one.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `tag invalid: ... cannot be overwritten because the repository is immutable` | The push reuses an existing tag | Push with a new, unique tag |
| CI works on the first run and fails on every rerun | The pipeline pushes a fixed tag such as `latest` or a branch name | Tag with commit SHA or build number |
| `AccessDeniedException` when running `put-image-tag-mutability` | The caller lacks `ecr:PutImageTagMutability` | Use an administrative role, or add the permission deliberately |
| A tag points at different code than before despite immutability | The image was deleted and the tag re-pushed | Restrict `ecr:BatchDeleteImage` and deploy by digest |
Exercise
Your team's pipeline pushes my-app:latest and my-app:<git-sha> on every build. You switch the repository to IMMUTABLE. What happens on the next build, and how would you change the pipeline so it keeps working while still giving deployments a stable reference?
Show answer
The <git-sha> push succeeds, because that tag is new. The latest push fails with an ImageTagAlreadyExistsException error (once latest has been pushed at least once), because immutability applies to every tag, including latest. Remove the latest push from the pipeline and deploy using the <git-sha> tag, or better the digest. If you need a "current" pointer, store the SHA or digest in a deployment configuration or parameter and update that value when you promote a build.
Exercise
Name two actions, other than pushing, that someone could use to make an immutable tag point at different content, and say how you would limit them.
Show answer
They can call ecr:BatchDeleteImage to delete the image and then push a different image with the same tag, or call ecr:PutImageTagMutability to switch the repository to MUTABLE, overwrite the tag, and switch it back. Limit both with IAM: grant these actions only to a small administrative role, and add an explicit Deny for other roles on the repository's ARN. Deploying by digest also removes the exposure, since a digest cannot be reassigned.