SDLC Automation · Topic 2 of 6
CodeBuild and the Buildspec File
Write a correct CodeBuild buildspec with phases, variables, secrets, caching and reports, and choose the right build environment and network settings.
- 4 min
- The default file is
buildspec.ymlat the source root; an override path is allowed. - Use buildspec version 0.2.
- Phases run as install, pre_build, build, post_build; post_build runs even if build failed.
What it is
AWS CodeBuild is a fully managed build service. It starts a fresh container for each build, runs your commands, uploads the output and then discards the container, so you pay only while builds run and have no build servers to patch.
The instructions live in a YAML file called the buildspec. By default CodeBuild looks for buildspec.yml in the root of the source, but you can set another path or paste the buildspec into the project.
How it works
Buildspec structure
The file has a version, optional environment settings, a set of phases, and sections for reports, artifacts and cache. Use version 0.2, which runs each command in the same shell session.
version: 0.2
env:
variables:
STAGE: test
parameter-store:
API_URL: /myapp/test/api-url
secrets-manager:
DB_PASSWORD: myapp/db:password
phases:
install:
runtime-versions:
nodejs: 18
commands:
- npm ci
pre_build:
commands:
- npm run lint
build:
commands:
- npm test
- npm run build
post_build:
commands:
- echo "Build succeeding flag is $CODEBUILD_BUILD_SUCCEEDING"
reports:
unit-tests:
files:
- junit.xml
file-format: JUNITXML
artifacts:
base-directory: dist
files:
- '**/*'
cache:
paths:
- node_modules/**/*
The four phases run in order: install, pre_build, build and post_build. The post_build phase still runs after a failed build phase, so check the CODEBUILD_BUILD_SUCCEEDING variable (1 or 0) before publishing anything from it (the build is still reported as failed).
Secrets and configuration
Plain values go under variables. Sensitive values should come from AWS Systems Manager Parameter Store (parameter-store) or AWS Secrets Manager (secrets-manager), so they never appear in the buildspec or in logs. The build role needs permission to read them.
A Secrets Manager reference has the form secret-id:json-key, with optional version fields after it.
Environment and compute
| Choice | Options |
|---|---|
| Image | AWS managed images or your own image from Amazon Elastic Container Registry (ECR) |
| Compute | A range of memory and CPU sizes, including Arm and GPU types |
| Privileged mode | Required to run the Docker daemon, for example to build container images |
| Service role | The IAM role CodeBuild assumes for S3, logs, ECR and other calls |
If a build must reach a private database or a private package repository, place the project in a Virtual Private Cloud (VPC). The VPC needs a route to the internet through a NAT gateway, or VPC endpoints, for the build to download dependencies or reach AWS APIs. A VPC build without that route is a classic timeout cause.
Caching
Caching shortens builds by reusing downloaded dependencies.
| Cache type | Use |
|---|---|
| Amazon S3 | Shared across build hosts, survives between builds, good for dependency folders |
| Local: source | Caches Git metadata |
| Local: Docker layer | Reuses image layers for container builds |
| Local: custom | Caches paths you list in the buildspec |
Reports
Test and code coverage reports are collected from files your tests write. JUnit XML is a common test format, and coverage formats include JaCoCo and Cobertura. Reports appear in report groups in the console, which gives trend history.
Build output to ECR
A container build usually logs in to ECR, builds, tags and pushes the image in the build and post_build phases. The role needs ECR push rights and privileged mode must be on.
aws codebuild start-build --project-name my-project
aws codebuild batch-get-builds --ids my-project:1234abcd
Batch builds
A batch build runs several builds from one request, either as a list, a graph with dependencies or a matrix. It is used for multi platform builds or parallel test shards.
What to remember for the exam
- The default file is
buildspec.ymlat the source root; an override path is allowed. - Use buildspec version 0.2.
- Phases run as install, pre_build, build, post_build; post_build runs even if build failed.
- Use
parameter-storeandsecrets-managerfor secrets, never plain variables. - Docker image builds need privileged mode.
- VPC builds need NAT or VPC endpoints to reach the internet and AWS services.
- S3 caching shares dependencies across builds; local caching is per host.
- The service role, not the caller, grants the build its permissions.
- Reports collect test and coverage results into report groups.
If a build can run docker commands locally but fails in CodeBuild with a daemon connection error, privileged mode is switched off.
Common mistakes
Putting secrets in variables. They are visible in the console and can leak into logs, so use Parameter Store or Secrets Manager.
Forgetting the post_build behavior. A push to a registry in post_build will run after a failed test unless you check the success flag.
Ignoring the VPC network path. Attaching a project to a VPC without NAT or endpoints makes dependency downloads hang.
Paying for builds that rebuild everything. Without a cache, every build reinstalls every dependency from scratch.
Give the CodeBuild service role only the permissions the build needs, because anyone who can edit the buildspec can run commands with that role.
Check yourself
Which buildspec section should hold a database password?
The
secrets-managersection underenv, which reads it at build time from AWS Secrets Manager.Why does a build in a private VPC time out downloading packages?
There is no route to the internet or to VPC endpoints, so a NAT gateway or endpoints are needed.
What setting lets CodeBuild run the Docker daemon?
Privileged mode on the build environment.
Topic 2 of 6 in SDLC Automation
Test yourself on SDLC Automation
The full AWS DOP-C02 question bank has 844 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.