Amazon ECR repositories have a lifecycle policy configured to automatically expire container images based on age, count, or tags.
Risk
Without lifecycle policies, images accumulate indefinitely, leading to:
- Availability issues when quotas block pushes and CI/CD
- Integrity risk from redeploying outdated, vulnerable images
- Cost growth from unnecessary storage
prowler aws --checks ecr_repositories_lifecycle_policy_enabled
Recommendation
Implement lifecycle policies per repository to expire untagged, old, or excess images and retain a small set of trusted releases. Validate outcomes before applying, review rules regularly, and apply consistently across Regions when replicating. This supports defense in depth by reducing image attack surface and operational risk.
Remediation
aws ecr put-lifecycle-policy --repository-name <REPOSITORY_NAME> --lifecycle-policy-text '{"rules":[{"rulePriority":1,"selection":{"tagStatus":"untagged","countType":"imageCountMoreThan","countNumber":1},"action":{"type":"expire"}}]}'
- Open the AWS Console and go to Amazon ECR > Repositories
- Select the target repository
- From Actions, choose "Lifecycle policies"
- Click "Create rule"
- Set Image status: Untagged, Match criteria: Image count more than = 1, Action: Expire
- Click "Save" to apply the lifecycle policy
Source Code
Resource Type
AwsEcrRepository
References
- https://docs.aws.amazon.com/AmazonECR/latest/userguide/LifecyclePolicies.html
- https://docs.aws.amazon.com/AmazonECR/latest/userguide/lp_creation.html
- https://aws.plainenglish.io/automation-deletion-untagged-container-image-in-amazon-ecr-using-ecr-lifecycle-policy-995eae2f5b8d
- https://blog.stackademic.com/title-implementing-lifecycle-policies-in-aws-ecr-a-practical-guide-3860b612b477
- https://www.trendmicro.com/trendaivisiononecloudriskmanagement/knowledge-base/aws/ECR/lifecycle-policy-in-use.html