Ravindra BagaleCourses & study guides मराठी Track your progress

AWS CodePipeline and CodeBuild: CI/CD on AWS Explained

Merging to main should not mean SSH and hope. CI/CD builds, tests, and deploys automatically. On AWS, CodePipeline orchestrates stages; CodeBuild runs your buildspec in managed containers — a cloud-native path alongside GitHub Actions.

What you'll learn in this chapter

  • Why CI/CD reduces fear of deploy
  • What CodePipeline and CodeBuild are
  • How stages, artifacts, and buildspec.yml work
  • When to pick AWS native vs GitHub Actions
  • Lab, errors, FAQ, interview

1. Everyday example first

A Noida team pushes to main. CodePipeline pulls from CodeCommit/GitHub, CodeBuild runs pytest + docker build, pushes to ECR, then deploys to ECS with a manual approval before prod. Failed tests stop the train.

2. Why CodePipeline and CodeBuild?

Why do we need this?

Manual deploys drift and skip tests. Pipelines encode the path: source → build → test → deploy with audit logs and IAM boundaries.

  • Repeatable builds in clean environments
  • Approvals for production
  • Artifacts between stages
  • Tight IAM for who can deploy

3. What are they?

What is it?

CodeBuild: managed build servers; you provide buildspec.yml (phases: install, pre_build, build, post_build). CodePipeline: connects sources (GitHub, CodeCommit, S3), build (CodeBuild), deploy (ECS, CodeDeploy, CloudFormation, hooks).

PiecePlain job
PipelineStage workflow engine
Source stagePull commits/artifacts
CodeBuild projectRun buildspec in container
Artifact storeUsually S3 zip between stages
Approval actionHuman gate before prod
buildspec.ymlDeclarative build steps
Git push → Pipeline source → CodeBuild → Deploy (with optional approval)Commit on main
Running version in env

4. How to write a minimal safe pipeline

How to do it (step by step)

  1. Store app in GitHub/CodeCommit; protect main branch.
  2. Add buildspec.yml with install/test/build; fail on test errors.
  3. Create CodeBuild project with privileged mode only if Docker needs it.
  4. Create Pipeline: Source → Build → Deploy.
  5. Use S3 artifact bucket with encryption.
  6. Add manual approval before prod deploy.
  7. Least-privilege service roles for Pipeline/Build/Deploy.
  8. Cache dependencies in CodeBuild for speed.
  9. Notify SNS/Chat on failure.
  10. Prefer immutable image tags (git sha) over latest.
# buildspec.yml sketch
version: 0.2
phases:
  install:
    commands: ["pip install -r requirements.txt"]
  build:
    commands: ["pytest -q", "docker build -t $REPO:$CODEBUILD_RESOLVED_SOURCE_VERSION ."]
artifacts:
  files: ["**/*"]

5. CodePipeline vs GitHub Actions

ChoiceWhen
CodePipeline+CodeBuildAWS deploy targets, IAM-centric audits
GitHub ActionsGitHub-centric teams; huge action ecosystem
HybridGHA build → deploy role to AWS
CodeDeployAgent-based rolling/blue-green to EC2/ECS/Lambda

6. Multi-industry notes

Banks require approval stages and change tickets linked in pipeline notes. Startups may start with GitHub Actions then add CodePipeline for regulated AWS accounts. Factories gate PLC-related software with extra test phases.

7. Industries and roles

  • DevOps/SRE: pipeline ownership
  • Fintech: gated prod deploys
  • SaaS: multi-env promote
  • EdTech: frequent safe releases
  • Consultants: standard pipeline templates

8. Lab: CodeBuild project + sample buildspec

Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.

How to do it (step by step)

  1. Create an S3 bucket for lab artifacts.
  2. Create CodeBuild project with a tiny GitHub/CodeCommit/S3 source (S3 zip ok for lab).
  3. Add buildspec that echoes hello and runs a trivial test command.
  4. Start build; confirm SUCCEEDED logs in CloudWatch.
  5. Optional: create Pipeline Source(S3)→Build.

Cleanup (stop billing)

  1. Delete pipeline, build project, lab bucket objects.
  2. Remove lab IAM roles.

9. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
latest tag alwaysUse git sha tags.
Admin rights on CodeBuild roleLeast privilege ECR/S3/logs.
Secrets in buildspec plaintextUse Secrets Manager/SSM; mask logs.
Skipped tests to go fasterFail pipeline on test fail.
No approval to prodAdd manual or automated gates.
Privileged mode always onOnly for Docker-in-Docker needs.
Mutable artifact overwritesVersion artifacts per commit.
Drifting console editsPipeline as Code (CFN/Terraform/CDK).
Ignoring failed stage notificationsSNS/ChatOps.
Building on shared unclean runners mentallyCodeBuild = fresh env each time — still pin versions.

10. Troubleshooting checklist

  1. Source auth fail: reconnect GitHub/CodeStar connection.
  2. Build fail: read CloudWatch log group carefully.
  3. Docker push deny: ECR auth token + IAM.
  4. Artifact too large: tighten files section.
  5. Pipeline stuck Pending: approval waiting.
  6. Clock/IAM eventual consistency after role change: retry.

11. FAQ

Q. CodeBuild free?

Pay per build minute; free tier may exist — check pricing.

Q. Need Jenkins?

Not required; many teams replace with managed CI.

Q. Windows builds?

Supported images exist for some workloads.

Q. Multi-account deploy?

Use assume-role deploy stages.

Q. Local buildspec test?

CodeBuild agent / local patterns exist.

Q. Blue/green?

CodeDeploy / ECS capabilities with pipeline.

12. Interview Q&A

Q. What is CodePipeline?

AWS CI/CD orchestrator connecting source/build/deploy stages.

Q. What is CodeBuild?

Managed build service driven by buildspec.yml.

Q. Artifact?

Output passed between pipeline stages (often S3).

Q. Why approvals?

Human control before irreversible prod changes.

Q. buildspec phases?

install/pre_build/build/post_build typical.

Q. GHA vs CodePipeline?

Ecosystem vs AWS-native IAM/deploy integration.

13. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

A green pipeline that never runs tests is theatre. Make pytest/npm test fail the build before any deploy stage.

Ravindra Bagale's Tip (MR)

Tests नसलेली green pipeline = theatre. Deploy आधी tests fail-the-build.

Tip (HI)

बिना टेस्ट की हरी pipeline सिर्फ नाटक है — deploy से पहले टेस्ट फेल करो।

Try at home

  1. Write an 8-line buildspec that runs unit tests then builds.
  2. List three IAM actions a build role might need for ECR push.
  3. Sketch Source→Build→Approval→Deploy.

In short

  1. CodePipeline orchestrates; CodeBuild executes builds.
  2. buildspec.yml defines clean, repeatable steps.
  3. Approvals + least privilege protect prod.
  4. Prefer immutable version tags.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: CodePipeline and CodeBuild. Manual zip-and-console deploys drift and cannot be audited. CodePipeline orchestrates stages (Source→Build→Deploy). CodeBuild runs buildspec builds in managed compute. Connect repo → CodeBuild project with buildspec → deploy action (ECS/CFN/CodeDeploy) → optional approval. Push to main builds image, runs tests, deploys to staging, waits for approval to prod.
Revision card: CodePipeline and CodeBuild · Download PNG · Open card page
Revision card: buildspec and gates. A pipeline without tests or approvals ships broken code to prod. buildspec.yml defines phases/artifacts. Manual approval and test actions are release gates. Fail the pipeline on test errors; require human approval before prod; store artifacts in S3. Unit tests red → Deploy never runs; prod stage waits on approver.
Revision card: buildspec and gates · Download PNG · Open card page
Comparison: CodePipeline vs CodeBuild. CodePipeline: Stages, transitions, approvals, multi-action flow. CodeBuild: Managed build containers running buildspec.
Comparison: CodePipeline vs CodeBuild · Download PNG · Open card page
Comparison: Automated test action vs Manual approval action. Automated test action: Fails pipeline on red tests/scans automatically. Manual approval action: Human must click before next stage.
Comparison: Automated test action vs Manual approval action · Download PNG · Open card page
Interview card: CodePipeline vs CodeBuild?. Pipeline = stage orchestration; CodeBuild = runs buildspec compute for compile/test/package. Pipeline invokes CodeBuild as a build action. Source→CodeBuild→Deploy ECS.
Interview card: CodePipeline vs CodeBuild? · Download PNG · Open card page
Interview card: What is buildspec.yml?. YAML that defines CodeBuild phases, commands, artifacts, and reports. Keep it in repo next to the app. pre_build tests; build docker push; artifacts image tag.
Interview card: What is buildspec.yml? · Download PNG · Open card page
Interview card: Why a manual approval stage?. Human gate before prod reduces blast radius of bad artifacts. Pair with automated tests earlier so approval is informed. Staging auto; prod waits for lead engineer.
Interview card: Why a manual approval stage? · Download PNG · Open card page
Interview card: What should fail the pipeline?. Failing unit/integration tests, security scans, or build errors must stop Deploy. Never continue-on-error into prod. npm test non-zero → Deploy skipped.
Interview card: What should fail the pipeline? · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

About this scenario

Shivneri Schools is a education business in Junnar. Kavita Shinde looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy List pipelines

Why: CI/CD inventory.

What to do: List pipelines.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List projects

Why: Build inventory.

What to do: List projects.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Pipeline automates release — 1

Why: CI/CD.

What to do: Enter 1 if CodePipeline automates releases.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy buildspec.yml drives build — 1

Why: Build.

What to do: Enter 1 if buildspec.yml drives CodeBuild.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Get pipeline

Why: Inspect stages.

What to do: Get pipeline shivnerischool-app.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Get pipeline state

Why: Which stage failed.

What to do: Get pipeline state shivnerischool-app.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Batch get projects

Why: Build details.

What to do: Batch get projects shivnerischool-build.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Start build

Why: Trigger.

What to do: Start build shivnerischool-build.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Allow StartBuild

Why: Builder LP.

What to do: Allow codebuild:StartBuild on arn:aws:codebuild:ap-south-1:110396552559:project/shivnerischool-build.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Stages JSON

Why: Source Build Deploy.

What to do: JSON stages Source,Build,Deploy.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Manual approval stage — 1

Why: Prod gate.

What to do: Enter 1 if a manual approval stage before prod is common.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Sample build timeout minutes

Why: Hang guard.

What to do: Sample build timeout minutes often used (enter 60)?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard List pipeline executions

Why: History.

What to do: List pipeline executions for shivnerischool-app.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard List builds for project

Why: Recent builds.

What to do: List builds for project shivnerischool-build.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Allow StartPipelineExecution

Why: Release LP.

What to do: Allow codepipeline:StartPipelineExecution on arn:aws:codepipeline:ap-south-1:110396552559:shivnerischool-app.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard buildspec version JSON

Why: YAML version.

What to do: JSON version 0.2.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.