Automating builds with CodeBuild and buildspec.yml

Automating Deployments on AWS

Dunieski Otano

Amazon Web Services Solutions Architect

What CodeBuild does

  • CodeBuild is a fully managed build service
  • No servers to provision or manage
  • Runs your build in a temporary container
  • Scales automatically; pay only for build minutes
  • Integrates with CodePipeline as the Build stage

 

1-codebuild-managed-service-temporary-container-lifecycle.png

Automating Deployments on AWS

The buildspec.yml file

  • buildspec.yml: YAML file at the root of your source code
  • Defines what CodeBuild does during the build
  • Four phases execute in order:
    • install: Install dependencies and tools
    • pre_build: Run setup tasks before building
    • build: Compile code, run tests
    • post_build: Package artifacts, clean up

buildspec

Automating Deployments on AWS

A real buildspec.yml example

  • install: pip install -r requirements.txt
  • pre_build: [ -z "$DB_URL" ] && exit 1
  • build: pytest tests/
  • post_build: zip -r app.zip . then echo "Build completed"

Light-theme code editor showing a buildspec.yml with four phases, where build runs pytest tests and post_build packages the code into app.zip, output as the pipeline artifact

Automating Deployments on AWS

Build environment configuration

 

4-codebuild-environment-config-compute-envvars-caching.png

  • Compute types: small, medium, large, 2xlarge
    • Choose based on build complexity and speed requirements
  • Environment variables: Pass configuration to builds
    • Store secrets in Parameter Store or Secrets Manager
  • Dependency caching: Cache downloaded packages between builds
    • Reduces build time significantly for large projects
Automating Deployments on AWS

Artifact outputs and pipeline integration

 

5-codebuild-artifact-output-s3-codepipeline-handoff.png

  • artifacts section defines what CodeBuild outputs
  • files: List of files or patterns; base-directory: root path
  • Output artifact stored in S3, passed to next stage automatically
  • Wrong artifact paths cause deploy failures even when build succeeds
Automating Deployments on AWS

The full build-test-deploy chain

  • Developer pushes code to CodeCommit
  • CodePipeline triggers automatically
  • CodeBuild: install -> pre_build -> build -> post_build
  • Build artifact flows to CodeDeploy
  • Application is deployed to the target environment

6-full-cicd-chain-codecommit-codebuild-phases-codedeploy.png

Automating Deployments on AWS

Let's practice!

Automating Deployments on AWS

Preparing Video For Download...