CodeBuild
CodeBuild runs your build and test commands on fresh, managed machines — no build servers to maintain.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 29, 2026
Related tutorials
Interview questions
Learning objectives#
- Write a buildspec.yml
- Use environment variables and secrets
- Build and push Docker images
- Speed up builds with caching
Explanation#
A build project defines the source, the build environment (image, compute size) and the service role.
buildspec.yml has phases: install, pre_build, build and post_build, plus artifacts and reports.
Environment variables can come from Parameter Store or Secrets Manager. Docker builds require privileged mode.
You pay per build minute; caching dependencies cuts both time and cost.
Code example: Test, build and push a Node.js image#
version: 0.2
env:
variables: { REPO: 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api }
phases:
install:
runtime-versions: { nodejs: 22 }
pre_build:
commands:
- npm ci
- npm test
- aws ecr get-login-password | docker login --username AWS --password-stdin ${REPO%%/*}
- TAG=${CODEBUILD_RESOLVED_SOURCE_VERSION:0:7}
build:
commands:
- docker build -t $REPO:$TAG .
post_build:
commands:
- docker push $REPO:$TAG
- printf '[{"name":"api","imageUri":"%s"}]' "$REPO:$TAG" > imagedefinitions.json
artifacts:
files: [imagedefinitions.json]
cache:
paths: ['/root/.npm/**/*']
[Container] Phase complete: PRE_BUILD State: SUCCEEDED [Container] Running command docker push .../shop-api:3f2a9c1 [Container] Phase complete: POST_BUILD State: SUCCEEDED
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- Node.js 22 is installed in the install phase.
- Tests run before any image is built — a failure stops the build.
- The image is tagged with the short commit SHA.
- imagedefinitions.json is produced for the ECS deploy stage.
Real-world use case#
CodeBuild is the Build stage of the CodePipeline in the previous lesson, or runs standalone for pull request checks.
Common mistakes#
Use the parameter-store or secrets-manager sections of the buildspec env.
Docker builds fail without it in standard CodeBuild images.
Best practices#
- Run tests before building images
- Tag images with commit SHAs
- Cache dependencies
- Give the service role only what the build needs
Remember this#
install → pre_build → build → post_build.
Docker in CodeBuild needs privileged mode.
Secrets via parameter-store / secrets-manager env.
Practice exercise#
Your task: Add an npm token from Secrets Manager secret ci/npm (key token) as NPM_TOKEN.
Show solution
env:
secrets-manager:
NPM_TOKEN: ci/npm:token
Mini quiz#
Which buildspec phase is typically used to push a Docker image?
Want more? Practise all AWS MCQs · AWS interview questions
My notes#
Saved in this browser.