Certopolis

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.0 always 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 Scan event 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

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A company's release process tests a container image in a staging environment and then deploys it to production with Amazon ECS. The production task definition must start containers from exactly the image content that passed testing, even if someone later pushes a different image to the Amazon ECR repository under the same tag. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Testing and Deployment building.

    Not that one: C is right. In the game your Testing and Deployment building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Referencing the tested image's digest in the task definition guarantees production runs exactly what staging tested.

    Written from docs.aws.amazon.com/AmazonECR/latest/userguide/docker-pull-ecr-image.html

  2. A build script on an Amazon EC2 instance authenticates the Docker CLI to the company's Amazon ECR private registry by running aws ecr get-login-password under an AWS CLI profile for an IAM role that can only pull images. Later, the script pushes a new image to a repository in the same registry, and the push is denied. The instance profile role is allowed to push to that repository. Which solution will resolve this issue?

    Right. In the game that’s Renown for your Testing and Deployment building.

    Not that one: D is right. In the game your Testing and Deployment building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Retrieving a new token with get-login-password under the instance profile role, and piping it into docker login for the registry, replaces the stored credential with one that carries push permissions.

    Written from docs.aws.amazon.com/AmazonECR/latest/userguide/registry_auth.html

  3. An Amazon ECR repository uses basic scanning with scan on push. A deployment pipeline must stop a release automatically when the pushed image has a CRITICAL finding, and it must make that decision as soon as the image's findings exist, without a scheduled job that checks the repository. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Testing and Deployment building.

    Not that one: B is right. In the game your Testing and Deployment building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A rule on that event invokes the function at the moment the findings exist, with no polling. Push events, whether ECR's own or CloudTrail's, fire when the push finishes, before the scan has produced anything to read.

    Written from docs.aws.amazon.com/AmazonECR/latest/userguide/image-scanning-basic.html

Ask for something