Certopolis

AWS Certified Developer – Associate

CodeBuild buildspec.yml phases, env and artifacts (DVA-C03)

The CodeBuild User Guide says a buildspec is a YAML file of build commands and settings. phases holds the commands for install, pre_build, build and post_build. env defines variables, including values read from Parameter Store and Secrets Manager. artifacts names the output files to upload, cache the paths to reuse, and reports the test result files.

What goes in each buildspec phase?

By default the file is named buildspec.yml and sits in the root of the source directory. A project uses one buildspec, but the name and location can be overridden, for example with buildspecOverride on start-build. version is required and AWS recommends 0.2. In version 0.1 each command runs in its own shell, so a cd or an exported variable does not carry to the next command.

phases is required. The allowed names are install, pre_build, build and post_build. AWS recommends install only for installing packages in the build environment, and it is where runtime-versions goes. pre_build is for steps such as signing in to Amazon ECR or installing npm dependencies. post_build is for steps such as packaging a JAR or pushing a Docker image to Amazon ECR.

Commands in a phase run one at a time in the order listed. If any command fails, the phase fails. A finally block runs after commands even when a command failed. on-failure sets what happens next: ABORT, CONTINUE or a RETRY form. on-failure is not supported on Lambda compute or reserved capacity.

How does env read secrets?

env/variables holds plain-text key and value pairs. AWS discourages putting sensitive values there, because environment variables can be displayed in plain text in the CodeBuild console and the AWS CLI.

env/parameter-store maps a local variable name to the name of a Systems Manager Parameter Store parameter. The CodeBuild service role must have the ssm:GetParameters action, or the build cannot read the value. env/secrets-manager maps a local variable name to secret-id:json-key:version-stage:version-id. Only the secret ID is required. Without a JSON key CodeBuild retrieves the whole secret text, and without a version it retrieves AWSCURRENT.

CodeBuild hides strings that come from Parameter Store and Secrets Manager in the build logs. The masking matches the exact stored value, so a value that the build Base64-encodes or changes in another way can still appear in the logs.

When the same variable is set in several places, the value on the start build call wins, then the build project definition, then the buildspec. env/exported-variables lists variables that CodePipeline passes to later stages. Parameter Store and Secrets Manager values and names that start with AWS_ cannot be exported.

Artifacts, cache and reports

artifacts tells CodeBuild where the build output is and how to prepare it for upload to the S3 output bucket. artifacts/files is required when the block is present. Paths are relative to the original build location, and '**/*' means all files recursively. The block can be left out when the build only pushes a Docker image to Amazon ECR or only runs unit tests.

base-directory names a top-level directory to collect from. The directory itself is not included in the artifact, only its files and subdirectories. discard-paths: yes flattens every file into one output directory. Example: with base-directory: dist and files: '**/*', the artifact root holds index.html and assets/, not a dist folder.

cache/paths lists the locations CodeBuild saves and restores between builds. A project can use Amazon S3 caching or local caching, and local caching has source, Docker layer and custom modes. cache/key is matched exactly, and up to five fallback-keys are matched by prefix.

reports maps a report group name or ARN to the test result files. The default file-format is JUNITXML. A project can have at most five report groups.

Where the exam catches people

  • Believing a successful build always hands an artifact to the next pipeline stage: CodeBuild uploads only what the artifacts block lists under files. A cache or reports block does not create a build output artifact.
  • Believing env/parameter-store works with no extra permission: the CodeBuild service role needs ssm:GetParameters on the parameter.
  • Believing a Base64-encoded token in env/variables is protected: plain-text variables can be displayed in the console and the AWS CLI. Use env/secrets-manager or env/parameter-store.
  • Believing files: 'dist/**/*' puts the contents of dist at the artifact root: the dist folder stays in the paths unless base-directory: dist is set.
  • Believing the buildspec value overrides the project setting: for a variable with the same name, the start build call wins, then the project, and the buildspec is last.

Written from these sources

Try 3 questions on this

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

  1. A developer configures an AWS CodeBuild project that compiles a Java application into a JAR file under the target directory. The build succeeds, but the following AWS CodePipeline deploy stage reports that no input artifact is available. Which solution will resolve this issue?

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

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

  2. An AWS CodeBuild project calls a third-party service during the build and needs an API token to do so. The token is already stored in AWS Secrets Manager. The token value must not appear in plaintext in the buildspec file or the project configuration, and CodeBuild must mask the value in the build logs. How should the developer configure the buildspec file to 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

    No secret material is stored in the buildspec, and CodeBuild hides strings that come from Secrets Manager in the build logs.

    Written from docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

  3. An AWS CodeBuild project reads a database password from an AWS Systems Manager Parameter Store SecureString parameter through the env/parameter-store block of its buildspec file. Every build fails early with an access denied error that names the parameter. The parameter exists and its value is correct. Which solution will resolve this issue?

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

    Not that one: A 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

    Granting it lets the build resolve the value, which CodeBuild then masks in the build logs.

    Written from docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

Ask for something