applying ECR lifecycle policy

Photo by Greg Rakozy on Unsplash

applying ECR lifecycle policy

etc/scripts

  • outline

When you create an ECR Repository, LifeCycle rule is not applied by default, and it will be charged unexpectly. Below is a script that applies LifeCycle to delete more than 5 untagged images from the entire ECR of the account.

  • script ( ecr_retention_put.sh )
#!/bin/bash
for i in $(aws ecr describe-repositories --query 'repositories[*].repositoryName[]' --output text)
do
        aws ecr put-lifecycle-policy --repository-name $i --lifecycle-policy-text "file://policy.json"
done
  • policy.json
{
   "rules": [
       {
           "rulePriority": 1,
           "description": "remove",
           "selection": {
               "tagStatus": "untagged",
               "countType": "imageCountMoreThan",
               "countNumber": 5
           },
           "action": {
               "type": "expire"
           }
       }
   ]
}