Skip to content

Instantly share code, notes, and snippets.

@dgoade
Created March 30, 2022 19:47
Show Gist options
  • Save dgoade/adb6e5a463e4d65b9a6752c5507fecc8 to your computer and use it in GitHub Desktop.
Save dgoade/adb6e5a463e4d65b9a6752c5507fecc8 to your computer and use it in GitHub Desktop.
ECR Lifecycle Policies
resource "aws_ecr_repository" "repo" {
name = "mycompany"
}
resource "aws_ecr_lifecycle_policy" "expire_policy" {
repository = aws_ecr_repository.repo.name
policy = <<EOF
{
"rules": [
{
"action": {
"type": "expire"
},
"selection": {
"countType": "imageCountMoreThan",
"countNumber": 1,
"tagStatus": "tagged",
"tagPrefixList": [
"latest"
]
},
"description": "Keep the latest image",
"rulePriority": 10
},
{
"action": {
"type": "expire"
},
"selection": {
"countType": "imageCountMoreThan",
"countNumber": 7,
"tagStatus": "tagged",
"tagPrefixList": [
"master",
"main"
]
},
"description": "Keep a handful of images built from master or main",
"rulePriority": 20
},
{
"action": {
"type": "expire"
},
"selection": {
"countType": "imageCountMoreThan",
"countNumber": 5,
"tagStatus": "tagged",
"tagPrefixList": [
"prod-",
"qa-",
"staging-"
]
},
"description": "Keep enough images to rollback",
"rulePriority": 30
},
{
"action": {
"type": "expire"
},
"selection": {
"countType": "imageCountMoreThan",
"countNumber": 1,
"tagStatus": "tagged",
"tagPrefixList": [
"prod",
"qa",
"staging"
]
},
"description": "Keep currently running image",
"rulePriority": 40
},
{
"action": {
"type": "expire"
},
"selection": {
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 1,
"tagStatus": "untagged"
},
"description": "Expire any untagged image",
"rulePriority": 50
},
{
"action": {
"type": "expire"
},
"selection": {
"countType": "imageCountMoreThan",
"countNumber": 900,
"tagStatus": "any"
},
"description": "Expire all other images in FIFO order",
"rulePriority": 60
}
]
}
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment