Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set a lifecycle policy for an ECR for tagged with any prefix?

I want to set a lifecycle policy for my ECR. For all tagged images to have image count more than 1. It's currently set to Prefix and can't set it blank.

aws ecr policy console: enter image description here

Thanks in advance.

like image 254
zyrahjane Avatar asked Nov 24 '17 04:11

zyrahjane


People also ask

What is lifecycle policy in Amazon ECR?

Amazon ECR - Lifecycle Policy Rules 1 Amazon ECR lifecycle policies enable you to specify the lifecycle management of images in a repository. 2 A lifecycle policy is a set of one or more rules, where each rule defines an action for Amazon ECR. 3 The actions apply to images that contain tags prefixed with the given strings. ... More items...

How do I set up a lifecycle policy for images?

For Rule description, type a description for the lifecycle policy rule. For Image status, choose Tagged, Untagged , or Any . If you specified Tagged for Image status, then for Tag prefixes, you can optionally specify a list of image tags on which to take action with your lifecycle policy.

What is a lifecycle policy?

A lifecycle policy contains one or more rules, where each rule defines an action for Amazon ECR. This provides a way to automate the cleaning up of unused images, for example expiring images based on age or count. You should expect that after creating a lifecycle policy, the affected images are expired within 24 hours.

How to tag a microservice in ECR?

In our ECR, we are having multiple repositories for each microservice. while building the code (for anyone service), creating an image out of it and pushing to ECR, we tag it with keyword - dev and build-XXX-XXX-XXX-XXX.


2 Answers

Lifecycle policies on all tagged images is now supported in ECR

You can use it by selecting Any for "Image Status". Alternatively in JSON, you can create a rule with "tagStatus": "any":

{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}

like image 54
Richard Nguyen Avatar answered Dec 25 '22 09:12

Richard Nguyen


Unfortunately it doesn't seem to be possible at the moment. The only solution I see is to create an own policy for each of characters [a-zA-Z0-9] so that each of these policies match to tagged images prefixed with that letter. Like this:

Policy 1: when tagged and prefixed with "a", expire after 30 days
Policy 2: when tagged and prefixed with "b", expire after 30 days
Policy 3: when tagged and prefixed with "c", expire after 30 days
...

Quite insane approach, but it'd work.

To be able to use policy wisely, one should start using some known tag prefixes. For example, one could tag all test images with prefix test- and release images with release-. Then it'd be sufficient to create policies for just these two prefixes.

like image 33
ronkot Avatar answered Dec 25 '22 10:12

ronkot