Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an ECR Lifecycle Policy with CloudFormation

In order to limit the number of images in a repository, I'd like to define a Lifecycle policy. Since all the stack is defined with CloudFormation, I'd like to define this policy too.

For example, my policy could be "keep only the most recent 8 images, no matter if tagged or not".

like image 341
Ing. Luca Stucchi Avatar asked Feb 12 '19 12:02

Ing. Luca Stucchi


People also ask

How do I add a lifecycle policy to ECR?

To create a lifecycle policy (AWS Management Console)In the navigation pane, choose Repositories. On the Repositories page, on the Private tab, select a repository to view the repository image list. On the repository image list view, in the left navigation pane, choose Lifecycle Policy.

How do you create a life cycle policy?

To create a lifecycle ruleSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a lifecycle rule for. Choose the Management tab, and choose Create lifecycle rule.

What's the purpose of AWS ECR lifecycle policies?

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 your container images by expiring images based on age or count. You should expect that after creating a lifecycle policy, the affected images are expired within 24 hours.

What is Lifecycle policy?

A document lifecycle policy is an object that defines the "life" of a document in terms of phases or states through which the document cycles. The states are user-defined, and, when one state moves to another, a lifecycle-related system event fires.


3 Answers

The solution was pretty easy, but since I could not find any example or similar questions (ECR is not mainstream, I know), let me post here the easy solution that I found, which simply requires to insert the policy as JSON into the CloudFormation definition:

MyRepository:
  Type: AWS::ECR::Repository
  Properties:
    LifecyclePolicy:
      LifecyclePolicyText: |
        {
          "rules": [
          {
            "rulePriority": 1,
            "description": "Only keep 8 images",
            "selection": {
              "tagStatus": "any",
              "countType": "imageCountMoreThan",
              "countNumber": 8
            },
            "action": { "type": "expire" }
          }]
        }

Of course this is very simplistic, but it's the starting point that I was looking for

like image 76
Ing. Luca Stucchi Avatar answered Sep 21 '22 09:09

Ing. Luca Stucchi


You can also define a reference to your PolicyText and later on your parameters.json stringify your policy.

It would look like something like this:

template.yml

Parameters:    
  lifecyclePolicyText:
    Description: Lifecycle policy content (JSON), the policy content the pre-fixes for the microservices and the kind of policy (CountMoreThan).  
    Type: String
  repositoryName:
    Description: ECR Repository Name to which we will apply the lifecycle policies. 
    Type: String
  registryId:
    Description: AWS account identification number (12 digits)
    Type: String
    Default: xxxxx
Resources:
  Repository:
    Type: AWS::ECR::Repository
    Properties:
      LifecyclePolicy:
        LifecyclePolicyText: !Ref lifecyclePolicyText
        RegistryId: !Ref registryId
      RepositoryName: !Ref repositoryName
Outputs:    
  Arn:
    Value: !GetAtt Repository.Arn

parameters.json

[
    {
      "ParameterKey": "lifecyclePolicyText",
      "ParameterValue": "{'rules':[{'rulePriority':1,'description':'Only keep 8 images','selection':{'tagStatus':'any','countType':'imageCountMoreThan','countNumber':8},'action':{'type':'expire'}}]}"
    }, 
    {
      "ParameterKey": "repositoryName",
      "ParameterValue": "xxxx"
    }
  ]
   
like image 33
Esteban Echavarrìa Avatar answered Sep 17 '22 09:09

Esteban Echavarrìa


| will allow you to add text inline.

AWSTemplateFormatVersion: "2010-09-09"
Resources:
    ECRRepo: 
      Type: AWS::ECR::Repository
      Properties: 
        RepositoryName: "images"
        LifecyclePolicy:
          LifecyclePolicyText: |
            {
                "rules": [
                    {
                        "rulePriority": 2,
                        "description": "Keep only one untagged image, expire all others",
                        "selection": {
                            "tagStatus": "untagged",
                            "countType": "imageCountMoreThan",
                            "countNumber": 1
                        },
                        "action": {
                            "type": "expire"
                        }
                    }
                ]
            }    
like image 33
ddtraveller Avatar answered Sep 21 '22 09:09

ddtraveller