備忘録です。
ご指摘等ありましたら教えていただけると幸いです。
最近現場で、新システムのインフラにECSの導入→コード化をしています。
latest運用にしており、untaggedイメージが増産されていました。
なのでサイクルを決めて削除するように変更しました。
下記コードは、
・ 1日のサイクルでuntaggedを削除
・ 1つだけイメージを残す
ecr.tf
# 作成済みECR
data "aws_ecr_repository" "app" {
name = local.service_name
}
# untaggedイメージの削除
resource "aws_ecr_lifecycle_policy" "app" {
repository = data.aws_ecr_repository.app.name
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Expire images older than 1 days",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 1
},
"action": {
"type": "expire"
}
}
]
}
EOF
}
参考文献
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecr_lifecycle_policy
https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/lifecycle_policy_examples.html