CI/CD with CodeBuild and CodePipeline
CI/CD automates the path from a git push to production: every change is built, tested and deployed the same way, in minutes, with no manual steps to forget.
flowchart LR
G[GitHub repo main branch] -->|push| S[Source stage]
S --> B["Build stage – CodeBuild<br/>install · test · build · package"]
B --> T[Deploy to staging]
T --> AP{Manual approval}
AP --> P["Deploy to production<br/>ECS / Lambda / S3 / CloudFormation"]
Concepts
| Concept | Meaning |
|---|---|
| Continuous integration | Every commit is built and tested automatically |
| Continuous delivery | Every passing build is ready to release (with approval) |
| Continuous deployment | Every passing build goes to production automatically |
| Pipeline | Ordered stages (Source, Build, Test, Deploy) of actions |
| Artifact | Files passed between stages (zip, image tag, template) |
AWS CodePipeline
- Source actions: GitHub/GitLab/Bitbucket via CodeConnections, Amazon S3, Amazon ECR.
- Build/Test actions: CodeBuild (or Jenkins).
- Deploy actions: CloudFormation, ECS (rolling / blue-green via CodeDeploy), Lambda, S3, Elastic Beanstalk.
- Approval actions pause for a human; pipelines trigger on push, PR merge or a schedule.
AWS CodeBuild and buildspec.yml
CodeBuild runs your build in a fresh, managed container – you pay per build minute.
version: 0.2
env:
variables:
NODE_ENV: test
phases:
install:
runtime-versions:
nodejs: 22
commands:
- npm ci
pre_build:
commands:
- npm run lint
- npm test
build:
commands:
- npm run build
post_build:
commands:
- aws s3 sync dist/ s3://notes-site-prod --delete
reports:
unit-tests:
files: ['junit.xml']
file-format: JUNITXML
artifacts:
base-directory: dist
files: ['**/*']
cache:
paths: ['node_modules/**/*']
| Phase | Typical commands |
|---|---|
install |
Choose runtimes, install dependencies |
pre_build |
Lint, unit tests, log in to ECR |
build |
Compile, bundle, docker build |
post_build |
Push image, upload artifacts, notify |
A failing command fails the phase and the build – the pipeline stops before deployment. The service role of the project needs permissions for anything the build does (S3 sync, ECR push).
Deployment strategies
| Strategy | How |
|---|---|
| Rolling | Replace instances/tasks gradually |
| Blue/green | Deploy a new environment, switch traffic, keep old for instant rollback |
| Canary / linear | Shift 10 % → 100 % of traffic while watching alarms (Lambda aliases, CodeDeploy) |
Build once and promote the same artifact through environments; run tests in the pipeline; keep infrastructure changes in the pipeline too (CloudFormation/Terraform).
Practise it
Virtual lab · Amazon Web Services · 40 min
CI/CD on AWS: test with CodeBuild and release with CodePipeline
Connect a GitHub repository, build and test it with a buildspec in CodeBuild, fix a failing test, then create a pipeline that builds and deploys every push to an S3 website bucket automatically.
Quick revision
- CI = build + test on every commit; CD = automated release path.
- CodePipeline orchestrates Source → Build → Deploy stages.
- CodeBuild runs
buildspec.ymlphases: install, pre_build, build, post_build. - Artifacts and reports flow between stages.
- Rolling, blue/green and canary deployments reduce risk.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.