Sub topic 7
Preventing tag overwrites with tag immutability
This chapter assumes you can build, tag, and push an image to ECR as shown in the previous chapters. It also assumes you know the difference between a tag and a digest.
The problem: tags can move
A tag is a label that points to one image digest. By default, an ECR repository is mutable. If you push a different image with a tag that already exists, ECR moves the tag to the new image. The old image stays in the repository, but it becomes untagged.
That behavior causes problems in production:
- A deployment that references
my-app:1.0.0can run different code on different days. - A rollback to
1.0.0may not restore the code you shipped as1.0.0. - Two teams can overwrite each other's tags without noticing.
What tag immutability does
When you set a repository to IMMUTABLE, ECR rejects any push that uses a tag that already exists. A tag can be assigned once. Pushing a new tag still works, and so does pushing the same image again under a new name.
Turning it on
You can enable it when you create the repository, as in the first example of this tutorial. For an existing repository, change the setting:
aws ecr put-image-tag-mutability \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--region us-east-1The change affects only future pushes. Existing tags are not modified. You can switch back to MUTABLE with the same command.
Seeing it work
Push 1.0.0 once, change your code, rebuild, and push the same tag again:
docker build -t my-app:1.0.0 .
docker tag my-app:1.0.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0The second push fails with 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 API reports this as ImageTagAlreadyExistsException. The fix is to publish the new build under a new tag, such as 1.0.1.
Consequences for your workflow
Immutability changes how you name images. These habits work well:
- Tag every build with a unique value, such as a semantic version or the Git commit SHA.
- Do not rely on a moving tag like
latest. In an immutable repository,latestcan be pushed only once. - Deploy by a unique tag or by digest, so the deployed content is fixed.
Some teams still want a moving tag for convenience. ECR supports tag exclusion filters for this case. With an IMMUTABLE_WITH_EXCLUSION setting, tags matching a filter, such as latest, stay mutable while every other tag is locked. Check the current AWS CLI reference for the exact parameters, because this option is newer than the basic setting.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Push fails with `ImageTagAlreadyExistsException` | The tag exists and the repository is immutable | Use a new tag |
| CI pipeline fails when it pushes `latest` a second time | The pipeline assumes mutable tags | Tag by commit SHA, or use an exclusion filter |
| Tag moved even though you expected it to be locked | The repository is still `MUTABLE` | Check `imageTagMutability` with `aws ecr describe-repositories` |
Exercise
Your repository is immutable and already contains my-app:2.1.0. A teammate fixes a bug and wants to publish it under the same tag. What happens if they run docker push with that tag, and what should they do instead?
Show answer
ECR rejects the push with ImageTagAlreadyExistsException, because the tag already exists in an immutable repository. They should build the fix and push it under a new unique tag, such as 2.1.1 or the commit SHA, and then update deployments to use it.
Exercise
A user still has the ecr:BatchDeleteImage permission in an immutable repository. Can they replace the content behind my-app:2.1.0? Explain.
Show answer
Yes. Immutability blocks overwriting a tag, but not deletion. The user can delete the image, which frees the tag, and then push a different image with that tag. To prevent this, remove ecr:BatchDeleteImage from users who do not need it.