AWS · Containers & CI/CD

CodeBuild

CodeBuild runs your build and test commands on fresh, managed machines — no build servers to maintain.

Advanced 5 min Lesson 22 of 30
0/30 in AWS
On this page · 5 min · Advanced

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#

YAMLbuildspec.yml Illustrative
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/**/*']
Expected output
[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#

  1. Node.js 22 is installed in the install phase.
  2. Tests run before any image is built — a failure stops the build.
  3. The image is tagged with the short commit SHA.
  4. 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#

Secrets as plain environment variables

Use the parameter-store or secrets-manager sections of the buildspec env.

Forgetting privileged mode for Docker

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
YAML Illustrative
env:
  secrets-manager:
    NPM_TOKEN: ci/npm:token

Mini quiz#

CodeBuild quizMultiple choice Question 1 of 1

Which buildspec phase is typically used to push a Docker image?

Score so far: 0/0

Want more? Practise all AWS MCQs · AWS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.