Automates applying AWS ECR image retention policies across multiple repositories using GitHub Actions. Includes a dry-run preview before any images are expired.
- Dry run — previews which images would be expired on the first 3 repos in
repos.txt - Apply — applies the policy to every repo in
repos.txt - Verify — confirms every repo has a policy attached
The apply job requires manual approval via a GitHub Environment (production) before it runs.
| Image type | Retention |
|---|---|
| Untagged | Expire after 1 day |
prod-* tagged |
Keep last 20 |
uat-* tagged |
Keep last 10 |
dev-* tagged |
Keep last 5 |
| Any other tagged | Keep last 5 |
Modify lifecycle-policy.json to change these rules.
Edit repos.txt and add one ECR repository name per line:
my-backend-service
my-frontend-app
my-worker-service
In your repo go to Settings → Secrets and variables → Actions and add:
| Secret | Value |
|---|---|
AWS_ACCESS_KEY_ID |
Your IAM user access key |
AWS_SECRET_ACCESS_KEY |
Your IAM user secret key |
In .github/workflows/apply-ecr-lifecycle.yml, update the region:
env:
AWS_REGION: us-east-2 # change to your regionGo to Settings → Environments → New environment, name it production, and add required reviewers. This gates the apply job behind a manual approval.
The IAM user needs these ECR permissions. See iam-policy.json for the policy — replace <YOUR_AWS_ACCOUNT_ID> with your account ID before applying.
{
"Action": [
"ecr:PutLifecyclePolicy",
"ecr:GetLifecyclePolicy",
"ecr:DeleteLifecyclePolicy",
"ecr:StartLifecyclePolicyPreview",
"ecr:GetLifecyclePolicyPreview",
"ecr:DescribeRepositories"
]
}Automatic — push a change to lifecycle-policy.json or repos.txt on main. The dry run runs first; apply runs after approval.
Manual — go to Actions → Apply ECR Lifecycle Policies → Run workflow and choose dry-run or apply.
Preview what would be expired for a single repo:
export AWS_REGION=us-east-2
./dry-run.sh my-backend-serviceApply to all repos:
./apply-lifecycle-policy.shVerify all repos have a policy:
./verify.sh| File | Purpose |
|---|---|
lifecycle-policy.json |
The ECR lifecycle policy applied to all repos |
repos.txt |
List of ECR repository names to target |
dry-run.sh |
Preview expiry for a single repo |
apply-lifecycle-policy.sh |
Apply policy to all repos in repos.txt |
verify.sh |
Confirm every repo has a policy |
iam-policy.json |
IAM policy template for the automation user |