Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
Set one lifecycle policy for all repositories in ECR

I curious if there is a way to set one common lifecycle policy, that will be applied to all repositories in ECR?

Currently, as I understand there is no way to do it.

One approach that I'm thinking about is to use JSON definition of lifecycle policies and apply it to all repositories with AWS CLI (can be a bit automated). But this thing should be run every time as a new repository is created that adds complexity.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

There is still no default ECR Lifecycle policy template or something. So, as you mentioned, you may use aws cli way, and assign this to execute from somewhere, like Lambda, or k8s job:

  1. Get all repositories names:

    repositories=($(aws ecr describe-repositories --profile=$profile --output text --query "repositories[*].repositoryName"))
    
  2. Apply policy to each repository:

    for repository in "${repositories[@]}";
    do
    aws ecr put-lifecycle-policy --profile=$profile --repository-name $repository --lifecycle-policy-text "file://policy.json"
    done;
    
over 4 years ago · Santiago Trujillo Report

0

you can use Terraform for that

resource "aws_ecr_lifecycle_policy" "untagged_removal_policy" {
count      = "${length(split(",",local.registries))}"
depends_on = [ "aws_ecr_repository.ecr_repositories" ]
repository = "${aws_ecr_repository.ecr_repositories.*.name[count.index]}"

policy = <<EOF
{
"rules": [
    {
        "rulePriority": 1,
        "description": "Expire Docker images older than 7 days",
        "selection": {
            "tagStatus": "untagged",
            "countType": "sinceImagePushed",
            "countUnit": "days",
            "countNumber": 7
        },
        "action": {
            "type": "expire"
        }
    }
]
}
EOF

}

over 4 years ago · Santiago Trujillo Report

0

I'm using CloudFormation mapping to define one policy and then apply it on all repositories with one line:

Mappings:
 ECRPolicy:
  DevPolicy:
    RemoveUntagged: |
      {
        "rules": [
          {
            "rulePriority": 1,
            "description": "Expire images older than 3 days",
            "selection": {
              "tagStatus": "untagged",
              "countType": "sinceImagePushed",
              "countUnit": "days",
              "countNumber": 3
            },
            "action": {
              "type": "expire"
            }
          }
        ]
      }

And for the repos it's just:

  ECRRepository:
   Type: AWS::ECR::Repository
   Properties:
    RepositoryName: !Sub ${ECRRepositoryName}-dev
    RepositoryPolicyText:
      Version: "2012-10-17"
      Statement:
        - Effect: Allow
          Action:
            - ecr:GetAuthorizationToken
            - ecr:BatchCheckLayerAvailability
            - ecr:GetDownloadUrlForLayer
            - ecr:GetRepositoryPolicy
            - ecr:DescribeRepositories
            - ecr:ListImages
            - ecr:DescribeImages
            - ecr:BatchGetImage
          Principal:
            AWS:
              - !Sub arn:aws:iam::${DevAccount}:root
          Sid: AllowCrossAccountPull
    LifecyclePolicy:
      LifecyclePolicyText: !FindInMap [ECRPolicy, DevPolicy, RemoveUntagged]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!