Subchapter 31.5
references/ecr.mdMarkdown6 KBView on GitHub
Domain expertise for managing ECR registries and repositories, as well as pushing container images and other artifacts. Covers various topics.
aws ecr create-repository \
--repository-name "$REPO_NAME" \
--image-scanning-configuration scanOnPush=true \
--image-tag-mutability IMMUTABLE \
--encryption-configuration encryptionType=AES256 \
--region "$REGION" \
--output jsonDeprecation notice:
--image-scanning-configurationis being deprecated in favor of registry-level scanning configuration viaput-registry-scanning-configuration(see Image Scanning section). The parameter still works but prefer the registry-level approach for new setups.
The operator SHOULD set:
scanOnPush=true to automatically scan images for vulnerabilities on push (or configure scanning at the registry level — see Image Scanning).image-tag-mutability IMMUTABLE to prevent tag overwriting. This ensures a given tag always refers to the same image digest. Use IMMUTABLE_WITH_EXCLUSION with --image-tag-mutability-exclusion-filters if specific tags (e.g., latest) must remain mutable.aws ecr get-login-password --region "$REGION" \
| docker login --username AWS \
--password-stdin "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com"Warning: The authentication token expires after 12 hours. The operator MUST re-authenticate before pushing if the token has expired. CI/CD pipelines SHOULD call
get-login-passwordat the start of every build.
docker build -t "$REPO_NAME:$IMAGE_TAG" .
docker tag "$REPO_NAME:$IMAGE_TAG" \
"$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:$IMAGE_TAG"
docker push \
"$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:$IMAGE_TAG"aws ecr describe-images \
--repository-name "$REPO_NAME" \
--image-ids imageTag="$IMAGE_TAG" \
--region "$REGION" \
--output jsonBasic scanning has no separate ECR charge (only enhanced scanning incurs Inspector charges).
# Trigger a manual scan
aws ecr start-image-scan \
--repository-name "$REPO_NAME" \
--image-id imageTag="$IMAGE_TAG" \
--region "$REGION" \
--output json
# Retrieve scan findings
aws ecr describe-image-scan-findings \
--repository-name "$REPO_NAME" \
--image-id imageTag="$IMAGE_TAG" \
--region "$REGION" \
--output jsonSee the relevant documentation (opens in a new tab) for more information.
Enhanced scanning provides continuous, automated scanning using Amazon Inspector. It covers OS packages and programming language packages.
The operator MUST enable enhanced scanning at the registry level:
aws ecr put-registry-scanning-configuration \
--scan-type ENHANCED \
--rules '[{"scanFrequency":"CONTINUOUS_SCAN","repositoryFilters":[{"filter":"*","filterType":"WILDCARD"}]}]' \
--region "$REGION" \
--output jsonEnhanced scanning incurs additional Inspector charges.
See the relevant documentation (opens in a new tab) for more information.
Amazon ECR lifecycle policies provide more control over the lifecycle management of images in a private repository. A lifecycle policy contains one or more rules, and each rule defines an action for Amazon ECR. Based on the expiration criteria in the lifecycle policy, images can be archived or expired based on the criteria specified in the lifecycle policy within 24 hours.
See the relevant documentation (opens in a new tab) for more information.
Note: sinceImagePulled cannot expire images. When a user wants to clean up images based on
last-pull time, be aware that the sinceImagePulled count type only works with the
transition action (to the archive storage class) — it cannot be used with expire.
To actually delete images by pull activity, use two rules: first transition them to
archive with sinceImagePulled, then expire them with sinceImageTransitioned. Archived
images must stay in archive for a minimum of 90 days before they can be deleted. Never
produce a policy that pairs sinceImagePulled with an expire action.
KMS via --encryption-configuration when you need key-level audit trail (KMS logs GenerateDataKey, Decrypt calls in CloudTrail) and customer-managed key rotation. AES256 (S3-managed keys) is the default. All ECR API calls are logged by CloudTrail regardless of encryption type.IMMUTABLE to prevent tag overwriting attacks (supply chain security). Use IMMUTABLE_WITH_EXCLUSION only when specific tags must remain mutable.ecr:GetAuthorizationToken requires Resource: "*" — it cannot be scoped to a repository.aws:PrincipalOrgID conditions in repository policies. Grant only ecr:BatchGetImage and ecr:GetDownloadUrlForLayer for pull-only access. Prefer specific role ARNs over :root principals.