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

AWS Cloud

AWS CodePipeline and CodeBuild: CI/CD on AWS Explained

Classroom chapter 249 of 249

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.

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?

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?

Think of a small college website stored in Git. The source stage captures a particular revision. The build stage runs commands such as tests and packages the result. A deployment stage takes the intended build output and updates the target. CodePipeline coordinates the stages and passes files between them. CodeBuild performs the build work in its own environment. The buildspec is the file that lists which commands to run and which files to return. Follow one revision through the three stages so you can tell which version actually went live.

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
Build and release a tested changeSource revision: A specific code change enters the pipeline; Build and test: CodeBuild produces checked artifacts; Approval, when configured: A reviewer checks release readiness; Deployment: Publishes the intended artifact; Verification: Check health and rollback conditionsSource revisionA specific code change enters thepipelineBuild and testCodeBuild produces checked artifactsApproval, when configuredA reviewer checks release readinessDeploymentPublishes the intended artifactVerificationCheck health and rollback conditions
Running version in env

4. How to write a minimal safe pipeline

Write one test with a known expected result. Before testing a release, change the sample so the test fails and watch where the pipeline stops. A useful failure log explains the task that failed and the revision. Fix the sample and repeat the run. Read the build output and confirm the deploy stage chooses the new tested artifact. If the build makes a container, record the image reference after pushing it to the registry so the deploy action can find the intended build.

  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 an encrypted S3 artifact bucket. The source action produces the input for CodeBuild. The buildspec lists files to package as the build output, which the deploy action can consume. For a container release, push the built image to the registry and pass the image reference expected by the deploy action.
  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

Use a sandbox build with a short message and a test assertion. Record the build ID, phase result and intended output filename on a successful run. Change one test to fail and confirm the test phase identifies the failure. In a lab pipeline verify that deployment did not run for that failed revision. Restore the test and confirm the next build succeeds. Delete the disposable project and its lab artifacts only.

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

  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.

Practise this chapter

Interview questions and answers

How would you prove a failed test cannot reach deployment?

Add one failing assertion to a disposable revision and start its pipeline. Read the build result and verify the deployment action never started for that revision. Restore the test and confirm the next successful build produced the intended artifact. Compare revision IDs so the evidence refers to the same work.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Which service executes install, test and build commands for the example?

Answer and explanation

B. CodeBuild

CodeBuild runs the build instructions. CodePipeline coordinates stages and sends their outputs to subsequent actions.

2. What does the artifact connect between pipeline stages?

Answer and explanation

A. The files produced by one action and consumed by a later action

An artifact carries the selected revision or build output. Trace the artifact to verify what the deployment used.

3. A build creates a container image only in its local environment. What else does a deploy action generally need?

Answer and explanation

A. The image published to a registry and the expected image reference

The deploy system must be able to retrieve the image and identify the intended version.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

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

CI/CD inventory.

List pipelines.

  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

Build inventory.

List projects.

  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 Apply the lesson: scenario 3

Which AWS service coordinates source, build and deployment stages in a release workflow?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy Apply the lesson: scenario 4

Using CodeBuild's default source-file convention, which filename contains the build commands?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Get pipeline

Inspect stages.

Get pipeline shivnerischool-app.

  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

Which stage failed.

Get pipeline state shivnerischool-app.

  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

Build details.

Batch get projects shivnerischool-build.

  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

Trigger.

Start build shivnerischool-build.

  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

Builder LP.

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

  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

Source Build Deploy.

JSON stages Source,Build,Deploy.

  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 Apply the lesson: scenario 11

A human must review a release before its production stage starts. Which CodePipeline action category fits this gate?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Sample build timeout minutes

Hang guard.

Sample build timeout minutes often used (enter 60)?

  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

History.

List pipeline executions for shivnerischool-app.

  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

Recent builds.

List builds for project shivnerischool-build.

  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

Release LP.

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

  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

YAML version.

JSON version 0.2.

  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.