Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

CodeCommit, CodeBuild and CodeDeploy

Practical study notes on AWS CodeCommit, CodeBuild and CodeDeploy: what each service is, how they connect in CI/CD, syntax and files to remember, worked examples, common errors and exam-style questions.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Software engineers build, test and ship code frequently. AWS CodeCommit, CodeBuild and CodeDeploy are three AWS services that together implement the source, build and deploy steps of a CI/CD pipeline. Knowing them helps you automate safe deployments to EC2, on-premises servers or Lambda and reduces manual errors during exams and real projects.

Key ideas

  • Deploy service: A system that takes build artifacts and places them into runtime environments, possibly with lifecycle hooks and health checks. AWS's deploy service is CodeDeploy. CodeDeploy uses an Application (logical), a Deployment Group (set of targets + deployment configuration), and an AppSpec file (typically appspec.yml) that maps artifacts to target locations and lists lifecycle hooks. Common deployment configurations include CodeDeployDefault.AllAtOnce, CodeDeployDefault.Linear10PercentEvery1Minute, and CodeDeployDefault.Canary10Percent30Minutes.

  • CI/CD flow assumptions: CodeCommit stores source → CodeBuild builds and outputs artifacts into an artifact store (S3 or CodePipeline-managed artifacts) → CodeDeploy pulls artifacts from S3 (or CodePipeline) and performs deployment steps on target compute (EC2, Auto Scaling group, on-prem, Lambda [AppSpec version 0.0], ECS [AppSpec version 1.0]).

  • Deployment strategies:

    • In-place (rolling): Update the existing instances in the deployment group one-by-one or in batches. Define: In-place deployment updates the application on the same host(s).
    • Blue/Green: Create a new environment (green), switch traffic from blue to green after validation. Define: Blue/Green deployment reduces downtime and risk by switching traffic to a new set of instances.
  • Permissions: Each AWS service needs a service role (an IAM role) that grants it rights to read from repositories or S3, to read/write logs, and to access deployment targets. A human (or automation) also needs a Git credential (HTTPS + Git credential helper or SSH key) or AWS IAM authentication method. Additionally, for EC2/on-prem deployments, each target instance must have an IAM instance profile that allows the CodeDeploy agent to read deployment bundles from your S3 bucket (for example, s3:GetObject) and to write logs to CloudWatch Logs.

  • Hooks and lifecycle events (AppSpec): CodeDeploy runs lifecycle hooks at named points (for EC2/on-prem: ApplicationStop, BeforeInstall, AfterInstall, ApplicationStart, ValidateService; for EC2/on‑prem blue/green also BeforeBlockTraffic, AfterBlockTraffic, BeforeAllowTraffic, AfterAllowTraffic). Hooks call scripts from the artifact and can control success/failure.

  • Test and rollback: Build should run unit tests. Deploy should include validation (a smoke test) and either automatic or manual rollback configuration. Define: rollback is returning systems to the previous known-good version when validation fails.

  • Tools to remember: Git commands (git clone, git add, git commit, git push), buildspec.yml structure, appspec.yml structure and deployment lifecycle hook names.

Draw a simple diagram for exams: draw three boxes left-to-right labeled CodeCommit, CodeBuild, CodeDeploy. From CodeCommit arrow to CodeBuild labelled "source". From CodeBuild arrow to S3 labelled "artifacts". From S3 arrow to CodeDeploy labelled "pull". From CodeDeploy arrow to EC2/On-prem/Lambda boxes labelled "deploy". Label lifecycle hooks near CodeDeploy box.

Formulas

Syntax lines to remember (each on its own line):

git clone <repo-URL> — clone repository.

git add <files>

git commit -m "message"

git push origin <branch>

Basic buildspec keys to remember:

version: 0.2 — buildspec version.

phases:

install:

commands:

pre_build:

build:

post_build:

artifacts:

files:

cache:

AppSpec (EC2/on-prem) minimum structure to remember:

version: 0.0

os: linux

files:

- source: /path/in/artifact

destination: /path/on/instance

hooks:

BeforeInstall:

- location: scripts/before_install.sh

Key CLI concepts:

aws codecommit create-repository --repository-name MyRepo

aws codebuild create-project --name MyProject --source ... --artifacts ... --environment ...

aws deploy create-application --application-name MyApp

(Exact long CLI JSON forms are not memorised here; remember conceptual flags: source, artifacts, environment, service role.)

Worked examples

Example 1 — Create CodeCommit repo and push code (step-by-step trace)

  1. Create repository (console or CLI): aws codecommit create-repository --repository-name student-app.
  2. Obtain HTTPS clone URL from console (example: https://git-codecommit.us-east-1.amazonaws.com/v1/repos/student-app).
  3. Local steps:
    • git init (if starting local repo)
    • git remote add origin https://git-codecommit...
    • git add .
    • git commit -m "first commit"
    • git push -u origin main

Explain expected outputs: git push shows counting objects, compressing objects, and ultimately "To https://.../student-app\n * [new branch] main -> main". If permission error appears, check IAM credentials or credential helper. In exams, write exact commands and explain permission failure cause. (Note: the remote URL shown matches exactly how you added origin; CodeCommit HTTPS URLs typically do not end with .git.)

Example 2 — Simple buildspec.yml and CodeBuild trace

Place this buildspec.yml in repo root (shortened):

version: 0.2 phases: install: commands: - echo "Installing..." pre_build: commands: - echo "Pre-build: setting environment" build: commands: - echo "Building..." - mvn -B package post_build: commands: - echo "Build complete" artifacts: files: - target/*.jar

Trace on a small Java project:

  • CodeBuild starts a container with chosen image.
  • Runs install commands: prints "Installing...".
  • Runs build commands: mvn -B package builds and creates JAR(s) under target/ (for example, target/myapp-1.0.0.jar).
  • Artifacts step uploads target/app.jar to configured S3 bucket.

Mark important exam notes: if artifacts not uploaded, check artifacts.files pattern and that CodeBuild project has an S3 bucket configured and a service role with s3:PutObject permissions. If using CodePipeline, set the CodeBuild artifacts type to CODEPIPELINE and ensure the CodePipeline service role grants access to the artifact store.

Example 3 — CodeDeploy AppSpec and lifecycle hooks (in-place deployment)

Sample appspec.yml:

version: 0.0 os: linux files:

  • source: / destination: /var/www/myapp hooks: ApplicationStop:
    • location: scripts/stop_server.sh timeout: 300 runas: root AfterInstall:
    • location: scripts/change_permissions.sh ApplicationStart:
    • location: scripts/start_server.sh ValidateService:
    • location: scripts/verify_health.sh

Step-by-step expected behavior:

  • CodeDeploy agent on each instance downloads artifact from S3.
  • Runs ApplicationStop script (stop old app), copies files, runs AfterInstall (permissions), starts service, then runs ValidateService to check health.
  • If ValidateService fails on configured percentage of instances, CodeDeploy can be configured to rollback.

Show how blue/green differs: a separate new Auto Scaling Group is created or registered targets are updated. Hooks BeforeAllowTraffic and AfterAllowTraffic can be used around the traffic switch.

Common mistakes

  • Mismatch between artifact path produced by build and appspec.yml source/files mapping → files not deployed.
  • Expecting CodeDeploy to pull source directly from CodeCommit — CodeDeploy normally pulls artifacts from S3 or gets input via CodePipeline. (You can integrate CodeCommit→CodeBuild→S3→CodeDeploy.)
  • Not installing or running the CodeDeploy agent on EC2/on-prem targets — then deployments fail with "agent not running".
  • Assuming default branch is main in all repos — some older or user-created repos may still use master.
  • Not using lifecycle hook timeouts — long-running scripts may be killed and cause false failures.

For your exam

Question types you will likely see: definitions (define CodeCommit/CodeBuild/CodeDeploy), short notes (describe buildspec or AppSpec), diagrams (draw flow of CI/CD among these services), commands (git push, AWS CLI create-project), and long questions asking for a step-by-step deployment of a sample app.

What a full-marks answer includes:

  • Clear definitions of each service and where it fits in CI/CD.
  • A simple labelled diagram showing CodeCommit → CodeBuild → S3 → CodeDeploy → targets.
  • Example buildspec.yml with phases and artifact block explained.
  • Example appspec.yml with hooks and explanation of in-place vs blue/green.
  • Notes on required IAM roles and common failure causes.
  • Where appropriate, exact commands and expected console output.

Quick check

  1. Name the buildspec phases in order.
  2. Where does CodeDeploy normally get deployment artifacts from?
  3. List two lifecycle hooks used in an EC2 in-place deployment.

Answers: install, pre_build, build, post_build; S3 (or via CodePipeline); BeforeInstall and AfterInstall (also ApplicationStart, ValidateService).

Note for the reviewer: I have tried to be exact about hook names and file structures. I state uncertainties below for verification; please correct any small differences in exact YAML keys or default service behaviours before publishing to students. Also I have simplified some CLI forms for clarity.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?