ECR lifecycle and repository policies - console guide

ECR lifecycle and repository policies - console guide

Amazon ECR is the hinge between CI/CD and EKS/ECS. Production setups combine immutable tags, scanning, lifecycle cleanup, and repository policies for cross-account pulls.

Policy JSON reference: ECR lifecycle policy · EKS ECR practices
Checklist: DevOps checklist


Use cases

NeedControl
Prevent tag overwriteImmutable tag mutability
Supply chainScan on push / enhanced scanning
Cost controlLifecycle expire untagged/old SHAs
Central registry accountRepository policy for spoke accounts

Console - create repository

  1. ECR → Create repository.
  2. Tag mutability: Immutable (prod).
  3. Scan on push: enable.
  4. KMS encryption: CMK optional.

Console - lifecycle policy

  1. Repository → Lifecycle policies → Create rule.
  2. Rules (separate per prefix - do not combine unrelated prefixes in one tagPrefixList):
PriorityRule
1Expire untagged images > 7 days
2Keep last 30 images tagged prod-*
3Expire feature-* > 14 days

See lifecycle doc for JSON.


Console - repository permissions (cross-account)

  1. Repository → Permissions → Edit.
  2. JSON policy allowing ecr:BatchGetImage, ecr:GetDownloadUrlForLayer for spoke account root or role.
{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "AllowPullFromProdAccount",
    "Effect": "Allow",
    "Principal": { "AWS": "arn:aws:iam::SPOKE_ACCOUNT:root" },
    "Action": [
      "ecr:BatchCheckLayerAvailability",
      "ecr:BatchGetImage",
      "ecr:GetDownloadUrlForLayer"
    ]
  }]
}

Spoke roles still need ecr:GetAuthorizationToken on *.


IAM for CI (push)

{
  "Effect": "Allow",
  "Action": [
    "ecr:GetAuthorizationToken",
    "ecr:BatchCheckLayerAvailability",
    "ecr:PutImage",
    "ecr:InitiateLayerUpload",
    "ecr:UploadLayerPart",
    "ecr:CompleteLayerUpload"
  ],
  "Resource": "*"
}

Scope PutImage to repository ARN when possible.


Security

  • Block public repositories (account setting).
  • Sign images (Notary/Cosign) in advanced pipelines.
  • Deny latest in prod deploy - pin digest or semver tag.

Commonly missed

MissImpact
Lifecycle prefix AND logicRule never matches
Immutable + deploy pipeline retags latestPipeline failures
No cross-account GetAuthorizationTokenPull ImagePullBackOff
Scan findings ignoredCVEs ship to prod

Troubleshooting

SymptomFix
ImageTagAlreadyExistsExceptionNew tag or mutable repo
Pull 403Repository policy + IAM on node role
Disk cost spikeLifecycle on untagged build layers

Docs: ECR lifecycle · Private repository policies

← Lifecycle JSON · Checklist