AWS Certified Developer – Associate
Amazon ECR tags vs digests, scanning and lifecycle (DVA-C03)
According to the Amazon ECR User Guide, an image is pulled as repository:tag or repository@digest. A tag can be overwritten unless the repository has tag immutability turned on. Docker authenticates with an authorization token that is valid for 12 hours and carries only the caller's IAM permissions. Scanning finds vulnerabilities, and lifecycle policies expire or archive images.
Tag or digest: which one pins an image?
The DVA-C03 exam guide lists building and managing container images, with Amazon ECR, Dockerfiles, image tagging and lifecycle policies as examples, as content added to Task 3.1.
The image name format is registry/repository:tag to pull by tag, or registry/repository@digest to pull by digest. A digest has the form sha256: followed by a hash. aws ecr describe-images shows the tags and the digest of each image in a repository.
In a mutable repository a push with an existing tag overwrites that tag. With tag immutability turned on, Amazon ECR returns ImageTagAlreadyExistsException for a push that reuses a tag. The setting is IMMUTABLE, or IMMUTABLE_WITH_EXCLUSION with filters for tags that may still be updated. AWS recommends mutable tags for repositories used by pull through cache, so that Amazon ECR can update cached images.
Example: to run exactly the image that staging tested, reference repository@sha256:... in the task definition. By default Amazon ECS itself resolves the tags in a task definition to digests during a deployment, so all tasks in the service run the same image.
How does Docker authenticate to a private registry?
The Docker CLI does not support IAM authentication natively. aws ecr get-login-password retrieves a regional token, and its output is piped into docker login --username AWS --password-stdin with the registry URI. The token is valid for 12 hours in the Region that issued it, and a token is needed for each registry.
The caller needs ecr:GetAuthorizationToken before it can authenticate. The token's permission scope matches the IAM permissions of the principal that requested it. A token gives no additional Amazon ECR permissions.
Example: a script logs in with a pull-only role and later pushes. The push is denied, even if another role on the host can push. Run get-login-password again with the role that can push and pipe it into docker login.
Image scanning and lifecycle policies
Basic scanning checks images for operating system vulnerabilities, either on push or by manual scan, and an image can be scanned once per 24 hours. Enhanced scanning uses Amazon Inspector, covers operating system and programming language packages, and offers scan on push and continuous scan. Findings are read with DescribeImageScanFindings.
With basic scanning, Amazon ECR sends an EventBridge event with the detail type ECR Image Scan when each scan completes. Its finding-severity-counts field holds a count for each severity level that has findings. A pipeline that must stop on a CRITICAL finding can use a rule on that event.
A lifecycle policy holds one or more rules. Each rule selects images by tagStatus (tagged, untagged or any) and a countType: imageCountMoreThan, sinceImagePushed, sinceImagePulled or sinceImageTransitioned. The action is expire, which deletes images, or transition, which moves them to archive storage. A lower rulePriority number is applied first, and a rule with tagStatus any must have the highest number. Images are expired or archived within 24 hours of meeting the criteria. AWS advises running the lifecycle policy preview before applying a policy.
Archived images cannot be pulled or scanned until they are restored. They have a minimum storage duration of 90 days, and a lifecycle policy cannot delete an image that has been in archive for less than 90 days.
Where the exam catches people
- Believing a version tag such as
1.4.0always points at the same image: in a mutable repository a later push can overwrite the tag. A digest reference,repository@sha256:..., names one image. - Believing an auth token carries the permissions of whichever role is on the host: the token has the permission scope of the principal that requested it.
- Believing a push event is the right trigger for reading scan findings: the
ECR Image Scanevent is sent when the scan completes, and it carries the severity counts. - Believing an archived image can be pulled for a rollback: it must be restored first.
- Believing a lifecycle rule with the lowest priority number is applied last: priority 1 is applied first.
Written from these sources
- Pulling an image to your local environment from an Amazon ECR private repository
- Preventing image tags from being overwritten in Amazon ECR
- Private registry authentication in Amazon ECR
- Scan images for software vulnerabilities in Amazon ECR
- Scan images for OS vulnerabilities in Amazon ECR
- Amazon ECR events and EventBridge
- Automate the cleanup of images by using lifecycle policies in Amazon ECR
- Lifecycle policy properties in Amazon ECR
- Examples of lifecycle policies in Amazon ECR
- Deploy Amazon ECS services by replacing tasks
- Comparison of DVA-C02 and DVA-C03