Stop 13 · Infrastructure as Code & CI/CDTopics · 7/9

AWS CodePipeline & CodeBuild

Part of the AWS Roadmap

In short

Some companies want their whole delivery pipeline inside AWS, using IAM, VPC access and AWS billing. CodePipeline strings the stages together, and CodeBuild runs the actual build and test commands.

CodePipeline is AWS's managed pipeline service that connects source, build, test and deploy stages, and CodeBuild runs build commands from a buildspec file in managed containers. Together they give a fully AWS-native CI/CD option.

The jaadu in a factory cap inspects bottles moving along a long conveyor belt past washing, filling and capping machines.

Think of it like this

The bottling plant line

At a juice plant near Nashik, bottles move along a conveyor: wash, fill, cap, label, quality check. The conveyor only moves things between stations, and each station does one job. If quality check fails, the bottle doesn't reach the truck. CodePipeline is the conveyor, CodeBuild is a work station, and the approval step is the quality inspector.

Watch it happen

A typical pipeline

Source
Build (CodeBuild)
Test
Manual approval
Deploy

1/5A push to GitHub, connected through AWS CodeConnections, starts an execution.

The big ideas

What you need to know

Stages and actions

A pipeline has stages, each with one or more actions. Artifacts pass between stages through an S3 bucket.

buildspec.yml

Lives in your repo and lists commands for install, pre_build, build and post_build phases, plus which files are artifacts.

Managed build images

CodeBuild provides standard Linux images with common runtimes, or you can bring your own image from ECR.

Versus GitHub Actions

GitHub Actions is more popular for code hosted on GitHub. CodePipeline fits teams that want everything in AWS or need private VPC access during builds.

See it in code

Walk through the code

yaml
1version: 0.22 3env:4  variables:5    REPO_URI: 111122223333.dkr.ecr.ap-south-1.amazonaws.com/shop-api6 7phases:8  pre_build:9    commands:10      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $REPO_URI11      - TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)12  build:13    commands:14      - npm ci15      - npm test16      - docker build -t $REPO_URI:$TAG .17  post_build:18    commands:19      - docker push $REPO_URI:$TAG20      - printf '[{"name":"api","imageUri":"%s"}]' $REPO_URI:$TAG > imagedefinitions.json21 22artifacts:23  files:24    - imagedefinitions.json

1/5buildspec version 0.2 is the current format.

A buildspec.yml for a Docker app. The project needs privileged mode turned on to run Docker builds.

Save yourself the pain

Common mistakes

Oops

Giving the CodeBuild service role full admin access.

Do this instead

It needs only what the build does: push to one ECR repo, write logs and read specific parameters.

Oops

Storing passwords as plain env variables in buildspec.yml.

Do this instead

Use env: secrets-manager or env: parameter-store so CodeBuild fetches them at build time.

Your mission

Build on every push

~45 min

Quick check

Where do you define the commands CodeBuild runs?

Go deeper

  1. 1Write a buildspec.yml that runs tests and builds a Docker image.
  2. 2Create a pipeline with a GitHub source connection, a CodeBuild stage and a deploy stage.
  3. 3Compare the setup with GitHub Actions and note when a team might choose each.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge