resource "aws_ecr_repository" "my_repository" {
  name                 = "${var.ecr_name}"
  image_tag_mutability = "MUTABLE"
  # Allows overwriting image tags, change to IMMUTABLE if you want to prevent overwriting

  image_scanning_configuration {
    scan_on_push = true
  }
}

resource "aws_ecr_lifecycle_policy" "my_repository_policy" {
  repository = aws_ecr_repository.my_repository.name

  policy = jsonencode({
    rules = [
      {
        rulePriority = 1
        description  = "Retain only the last 10 images"
        selection    = {
          tagStatus   = "any"
          countType   = "imageCountMoreThan"
          countNumber = 10
        }
        action = {
          type = "expire"
        }
      }
    ]
  })
}