Courseiva
SDLC AutomationeasyMultiple ChoiceObjective-mapped

DOP-C02 SDLC Automation Practice Question

A company uses AWS CodeBuild to build a Docker image and push it to Amazon ECR. The buildspec.yml includes a 'post_build' phase command to tag the image. The build fails with 'unauthorized: authentication required'. What must be done to resolve this?

⚠ Common exam trap

It's easy for candidates to assume the 'unauthorized' error is due to missing 'ecr:GetAuthorizationToken' or a need to run 'docker login', but the real issue is the absence of specific layer upload permissions required for the push operation.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Add 'ecr:InitiateLayerUpload' and 'ecr:CompleteLayerUpload' permissions to the CodeBuild service role.

The error 'unauthorized: authentication required' indicates that CodeBuild's IAM role lacks the necessary permissions to push the Docker image to Amazon ECR. The correct resolution is to add the specific ECR permissions 'ecr:InitiateLayerUpload' and 'ecr:CompleteLayerUpload' to the CodeBuild service role, as these are required for the Docker push operation to upload image layers. Without these permissions, the ECR API rejects the push even if other permissions like 'ecr:GetAuthorizationToken' are present.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Add 'ecr:InitiateLayerUpload' and 'ecr:CompleteLayerUpload' permissions to the CodeBuild service role.

    Why this is correct

    The AWS CodeBuild service role is the IAM identity that supplies temporary credentials to the build container. Pushing a Docker image to Amazon ECR requires calling the ECR API operations InitiateLayerUpload, UploadLayerPart, CompleteLayerUpload, and PutImage, so the role must explicitly allow those actions. Without these permissions, even a successfully authenticated docker login will fail when the push actually attempts to upload the image layers. Granting the policy to the service role is the secure, minimal-change fix.

  • Use the 'docker login' command with AWS CLI in the build phase.

    Why it's wrong here

    Running `docker login` with the AWS CLI in the build phase is a valid way to authenticate the Docker client with ECR, but it only obtains a registry authentication token. That token does not itself grant the underlying IAM permissions; every ECR API request made during the push is still authorized against the CodeBuild service role's policy. If the role is missing ecr:InitiateLayerUpload or ecr:CompleteLayerUpload, the login succeeds but the push fails with an access-denied error. Thus, the login command is not the missing piece—it is the role's permissions that need updating.

  • Install the AWS CLI in the CodeBuild build environment.

    Why it's wrong here

    The AWS CLI is already preinstalled in all current CodeBuild managed images, including those based on Amazon Linux 2 and the standard Docker images, so the installation step would be redundant. More importantly, the error is not caused by a missing CLI binary or helper tool; it is entirely an IAM authorization problem. Installing the CLI would not alter the permissions of the service role that CodeBuild uses, so the push would still fail. The fix must be a policy change on the IAM role, not a change to the build environment.

  • Create a new IAM user with ECR permissions and store the keys in CodeBuild environment variables.

    Why it's wrong here

    Creating a new IAM user and hard-coding its access keys in CodeBuild environment variables would technically give the build the ECR permissions it needs, but it violates AWS best practices by introducing long-term static credentials that can leak or expire. The CodeBuild service can instead assume an IAM role, which automatically supplies short-lived, rotating credentials to the build container; no key management is required. Additionally, storing secret keys in environment variables increases the risk of exposure via logs or build artifacts. The simplest and secure solution is to add the required ECR actions to the existing CodeBuild service role, which is the identity the build already uses.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

This DOP-C02 question is part of Courseiva's 251-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This DOP-C02 practice question is part of Courseiva's free Amazon Web Services certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the DOP-C02 exam.