Create an ECR repository with image scanning and a lifecycle policy
A private container registry that scans each image when it is pushed, never lets a tag be overwritten, and deletes old images on its own so the storage bill does not creep up.
docker push
ECR repository
Scan on push
Lifecycle cleanup
How it works
What goes wrong with a default repository
A repository created with the defaults keeps every image forever and lets anyone with push access replace the image behind a tag. After a year you are paying to store hundreds of images nobody will run again, and you cannot be sure that the tag you tested is the image in production.
Make tags immutable
With immutable tags, a second push to the same tag is rejected. The mistake people make is to keep pushing a tag called latest, which now fails on the second deploy. Tag each image with the git commit SHA instead, and deploy that tag.
Scan each image on push
ECR checks the operating system packages in every pushed image against a list of known vulnerabilities and attaches the findings to the image. The scan does not block the push. Read the findings with aws ecr describe-image-scan-findings, or have your pipeline fail when it reports a critical finding.
Expire old images
The lifecycle policy has two rules. Untagged images, which are mostly leftovers from interrupted pushes, are removed after seven days. After that, only the newest 30 images are kept, which is enough to roll back several weeks for most teams.
Push the first image
Log in with aws ecr get-login-password piped into docker login, then tag your image with the repository_url output and push it. Give the role that pushes only the image push actions on this one repository ARN.
What you set
repository_name
Name of the repository For example web-app
keep_image_count
How many recent images to keep For example 30
What it costs to run
Usually under $5 a month. ECR charges about $0.10 per GB stored each month, so 30 images of 500 MB with shared layers is a few dollars at most. Scan on push has no extra charge. Estimate.
The configuration
# An ECR repository for container images, with scanning and automatic cleanup.
#
# What this creates:
# - a private ECR repository with encryption and scan on push
# - immutable tags, so a tag always points at the image it was pushed with
# - a lifecycle policy that removes untagged layers and old images
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region to deploy into."
type = string
default = "us-east-1"
}
variable "repository_name" {
description = "Name of the repository, usually the name of the app."
type = string
}
variable "keep_image_count" {
description = "How many of the most recent images to keep. Older ones are deleted."
type = number
default = 30
}
variable "untagged_expiry_days" {
description = "Days before an untagged image is deleted."
type = number
default = 7
}
variable "force_delete" {
description = "Allow destroy to delete a repository that still has images. Leave false outside tests."
type = bool
default = false
}
resource "aws_ecr_repository" "main" {
name = var.repository_name
# A tag cannot be pushed twice. What you tested under a tag is what runs
# under that tag, and a rollback to an old tag gets the old image.
image_tag_mutability = "IMMUTABLE"
# With false, destroy refuses to remove a repository that has images in it.
force_delete = var.force_delete
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
}
# Rules run in priority order. Untagged images go first, then anything
# beyond the newest keep_image_count images.
resource "aws_ecr_lifecycle_policy" "main" {
repository = aws_ecr_repository.main.name
policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Remove untagged images after ${var.untagged_expiry_days} days"
selection = {
tagStatus = "untagged"
countType = "sinceImagePushed"
countUnit = "days"
countNumber = var.untagged_expiry_days
}
action = {
type = "expire"
}
},
{
rulePriority = 2
description = "Keep the newest ${var.keep_image_count} images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.keep_image_count
}
action = {
type = "expire"
}
},
]
})
}
output "repository_url" {
description = "Use this as the image name in docker push, followed by a colon and a tag."
value = aws_ecr_repository.main.repository_url
}
output "repository_arn" {
description = "Give this to the role that pushes images."
value = aws_ecr_repository.main.arn
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.