Skip to content

ECR Life cycle policy

0

I created below life cycle policy for ECR But all images other than latest 20 get expired. I want to keep latest 20 and the images within 6 month. to be clear - there can be more images than 20 if they ar within last six month

{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Latest",
      "selection": {
        "tagStatus": "tagged",
        "tagPrefixList": [
          "latest"
        ],
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 9999
      },
      "action": {
        "type": "expire"
      }
    },
    {
      "rulePriority": 2,
      "description": "Keep 20 most recent images",
      "selection": {
        "tagStatus": "tagged",
        "tagPatternList": [
          "*"
        ],
        "countType": "imageCountMoreThan",
        "countNumber": 20
      },
      "action": {
        "type": "expire"
      }
    },
    {
      "rulePriority": 3,
      "description": "Expire all images older than 180 days",
      "selection": {
        "tagStatus": "any",
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 1800
      },
      "action": {
        "type": "expire"
      }
    }
  ]
}```

asked a year ago712 views

1 Answer
0

You're right to be concerned about the second condition potentially expiring all images after 50. To create an ECR lifecycle policy that meets your requirements, you'll need to carefully structure your rules. Here's how you can approach this:

  1. To retain the latest image: Create a rule with the highest priority that keeps the image tagged as "latest".

  2. To keep the newest 50 images: Add a rule that retains the 50 most recent images, regardless of their tags.

  3. To keep images within the last 6 months: Include a rule that keeps images pushed within the last 180 days.

Here's a suggested structure for your lifecycle policy:

{
"rules": [
{
"rulePriority": 1,
"description": "Keep image with 'latest' tag",
"selection": {
"tagStatus": "tagged",
"tagPrefixList": ["latest"],
"countType": "imageCountMoreThan",
"countNumber": 1
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 2,
"description": "Keep newest 50 images",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 50
},
"action": {
"type": "expire"
}
},
{
"rulePriority": 3,
"description": "Keep images from last 6 months",
"selection": {
"tagStatus": "any",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 180
},
"action": {
"type": "expire"
}
}
]
}

This policy will work as follows:

  1. The first rule ensures that the image tagged "latest" is always kept.
  2. The second rule keeps the 50 most recent images, preventing them from being expired by the third rule.
  3. The third rule keeps all images pushed within the last 180 days (approximately 6 months).

Remember that in ECR lifecycle policies, rules are evaluated in order of priority (lower numbers are higher priority). An image that matches a higher priority rule cannot be expired by a lower priority rule. This way, your policy will retain the latest image, the newest 50 images, and all images from the last 6 months, whichever is greater.

Always use the lifecycle policy preview feature before applying the policy to ensure it behaves as expected with your specific set of images.
Sources
Automate the cleanup of images by using lifecycle policies in Amazon ECR - Amazon ECR
In ECR I created a lifecycle policy to keep images for 10 years but it deleted them instead | AWS re:Post
Examples of lifecycle policies in Amazon ECR - Amazon ECR

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.