Certopolis

AWS Certified DevOps Engineer – Professional

CodeBuild buildspec phases, finally blocks and env variables

A buildspec has four phases: install, pre_build, build and post_build. The CodeBuild User Guide says a finally block in a phase runs even when a command in that phase fails. For an environment variable defined in several places, the start-build value wins, then the project definition, then the buildspec.

What is each buildspec phase for?

AWS recommends install only for installing packages in the build environment, such as a test framework. pre_build holds commands that run before the build, for example signing in to Amazon ECR or installing npm dependencies. build runs the build or test tools. post_build runs after the build, for example packaging artifacts, pushing a Docker image to Amazon ECR or sending a notification through Amazon SNS.

By default the file is named buildspec.yml and sits in the root of the source directory. A project can name a different file with its buildspec setting, and a single build can do so with buildspecOverride on start-build. Both paths are relative to CODEBUILD_SRC_DIR. The file can also be in an S3 bucket in the same Region as the project. A build project has only one buildspec.

How do finally and on-failure change a failed phase?

When a command in a phase's commands list fails, CodeBuild skips the rest of that list and then runs the phase's finally block. A phase succeeds only when every command in both commands and finally succeeds. A cleanup command in finally therefore always runs, and the phase is still reported as failed.

on-failure sets what happens after a phase fails: ABORT stops the build, CONTINUE moves to the next phase, and the RETRY forms retry the build, up to 3 times by default or a count from 0 to 100, optionally only when the error matches a regular expression. on-failure is not supported on Lambda compute or reserved capacity.

Which value does an environment variable get?

A variable with the same name can be set in three places. The value passed in the start build call has the highest precedence. The value in the build project definition comes next. The value declared in the buildspec has the lowest precedence. Any variable you set also replaces one of the same name that the Docker image already defines.

Plain-text variables can be displayed in the console and the AWS CLI, so AWS discourages using them for sensitive values. The alternatives are the PARAMETER_STORE and SECRETS_MANAGER variable types on the project, or the env/parameter-store and env/secrets-manager mappings in the buildspec. For these types the variable's value is the name of the parameter or secret, and CodeBuild substitutes the stored value at run time.

Reading from Parameter Store requires the ssm:GetParameters action on the CodeBuild service role. Names that start with CODEBUILD_ are reserved.

Where the exam catches people

  • Believing a buildspec value overrides the project's value: the buildspec has the lowest precedence. Start build beats the project, and the project beats the buildspec.
  • Believing a finally block rescues a failed phase: it runs after a failure, but the phase still fails if any command failed.
  • Believing the remaining commands in a phase still run after one fails: CodeBuild skips them and goes to finally.
  • Believing a PARAMETER_STORE variable works without extra IAM: the service role needs ssm:GetParameters.
  • Believing one project can hold several buildspecs: it holds one. Use buildspecOverride on the build to choose a different file.

Written from these sources

Try 3 questions on this

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

  1. A DevOps team's AWS CodeBuild project builds a container image for an order service. Its buildspec signs in to Amazon ECR in the pre_build phase, and the build phase compiles the code, runs the unit tests and builds the image. The team now wants two more commands to run after the compile and test commands: one pushes the image to Amazon ECR, and one sends a message to an Amazon SNS topic. The two commands must sit in a phase of their own, so that the build log reports them separately. Which solution will meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

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

    Show the full explanation and doc quotes

    Putting the two commands there gives them their own phase in the build log and makes them run after the image has been built and tested.

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

  2. An AWS CodeBuild project runs integration tests in the build phase of its buildspec. The phase's first command creates a temporary Amazon RDS database for the tests, the second loads fixtures and the third runs the test suite. Failed runs have been leaving databases behind, and the bill for them is growing. A cleanup command must delete the temporary database on every run, whichever command fails, and the build must still be reported as failed whenever any of the commands fails. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

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

    Show the full explanation and doc quotes

    The phase succeeds only when both its commands and its finally block succeed, so a failed test still fails the build.

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

  3. A company has 30 AWS CodeBuild projects that share one buildspec file from a central repository. Each project definition holds API_ENDPOINT, the address of an internal test service, as a PLAINTEXT environment variable, so changing the address means editing 30 projects. The platform team has created the AWS Systems Manager Parameter Store parameter /ci/api-endpoint and wants every build to read the address from that parameter from now on. Build commands must keep reading API_ENDPOINT, and the projects' service roles have no Parameter Store permissions yet. Which solution will meet these requirements?

    Right. In the game that’s Renown for your SDLC Automation building.

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

    Show the full explanation and doc quotes

    Turning each project's API_ENDPOINT into a PARAMETER_STORE variable whose value is the parameter name keeps the variable name the build commands use and makes the project-level definition itself fetch /ci/api-endpoint at build time. The service roles need ssm:GetParameters for CodeBuild to read it, and after this one-time edit an address change is a single parameter update.

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

Ask for something