AWS for DevOps — CI/CD Practice Exercises
6.6 — CI/CD Practice Exercises
Test your understanding of CodeBuild, CodeDeploy, CodePipeline, deployment strategies, and GitHub integration.
Exercise 1 — buildspec.yml Debugging
A team has this buildspec.yml but their Docker image never gets pushed to ECR. The build shows SUCCEEDED. What is wrong?
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to ECR...
- aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
build:
commands:
- echo Building Docker image...
- docker build -t my-app .
post_build:
commands:
- echo Pushing image...
- docker tag my-app:latest 123456789012.dkr.ecr.ap-south-1.amazonaws.com/my-app:latest
- docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/my-app:latest
The CodeBuild project has privilegedMode=false.
Show Answer
Two issues:
-
privilegedModeisfalse— Docker-in-Docker requiresprivilegedMode=trueon the CodeBuild project. Without it,docker buildsilently fails but doesn’t stop the build. -
post_buildruns even ifbuildfails — Thepost_buildphase executes regardless of whether previous phases succeeded. The team should check$CODEBUILD_BUILD_SUCCEEDINGinpost_build:
post_build:
commands:
- |
if [ "$CODEBUILD_BUILD_SUCCEEDING" = "1" ]; then
docker tag my-app:latest 123456789012.dkr.ecr.ap-south-1.amazonaws.com/my-app:latest
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/my-app:latest
else
echo "Build failed, skipping push"
fi
Fix: Set privilegedMode=true on the project and add the CODEBUILD_BUILD_SUCCEEDING check.
Exercise 2 — CodeDeploy Lifecycle
Put these CodeDeploy lifecycle events in the correct order:
ApplicationStartValidateServiceBeforeInstallInstallApplicationStopAfterInstall
Which events can you write hooks for, and which are handled internally by CodeDeploy?
Show Answer
Correct order:
ApplicationStop— hook (your script)DownloadBundle— internal (CodeDeploy downloads revision from S3)BeforeInstall— hook (your script)Install— internal (CodeDeploy copies files perfiles:section)AfterInstall— hook (your script)ApplicationStart— hook (your script)ValidateService— hook (your script)
You can write hooks for: ApplicationStop, BeforeInstall, AfterInstall, ApplicationStart, ValidateService
Handled internally by CodeDeploy: DownloadBundle, Install
Exercise 3 — Choose the Deployment Strategy
For each scenario, recommend a deployment strategy and explain why.
| # | Scenario |
|---|---|
| A | A startup’s development environment — fast iteration, cost matters |
| B | A bank’s payment processing API — zero downtime, instant rollback required |
| C | An e-commerce platform serving 10 million requests/day — zero downtime, gradual validation needed |
| D | A batch processing worker that runs nightly — short downtime acceptable |
Show Answer
| # | Strategy | Why |
|---|---|---|
| A | In-place / All-at-once | Fast, cheap, downtime acceptable in dev |
| B | Blue/Green | Zero downtime + instant rollback by switching traffic back to Blue environment |
| C | Canary | Shift 5-10% traffic first, monitor error rates and latency, then shift 100% — minimises blast radius |
| D | In-place (OneAtATime) | Brief downtime is fine; simple and cost-effective for batch workers |
Key insight: Production APIs with high traffic should always use Canary or Blue/Green. Development environments can use simpler (and cheaper) strategies.
Exercise 4 — Pipeline Artefact Troubleshooting
A pipeline has three stages:
Source (GitHub) → Build (CodeBuild) → Deploy (CodeDeploy)
The Build stage succeeds, but Deploy fails with:
ActionExecution failed. Error: Artifact 'SourceOutput' not found
The pipeline JSON shows:
- Source action:
outputArtifacts: [{ "name": "SourceCode" }] - Build action:
inputArtifacts: [{ "name": "SourceOutput" }]
What is the problem and how do you fix it?
Show Answer
Problem: Artefact name mismatch.
The Source stage outputs an artefact named SourceCode, but the Build stage expects an artefact named SourceOutput. CodePipeline passes artefacts between stages by name — they must match exactly.
Fix: Make the names consistent:
// Source stage
"outputArtifacts": [{ "name": "SourceOutput" }]
// Build stage
"inputArtifacts": [{ "name": "SourceOutput" }]
Or change both to any matching name. The key rule: the outputArtifacts name of one action must exactly match the inputArtifacts name of the consuming action.
Exercise 5 — GitHub Integration Design
Your team wants to set up CI/CD for a microservice:
- Source code on GitHub (
mainbranch for releases, feature branches for PRs) - PRs: Run tests and report status back to GitHub ✅/❌
- Merge to main: Build → Deploy to staging → Manual approval → Deploy to production
- Use Blue/Green deployment for production
Design the AWS resources needed. List the services, how they connect, and any key configuration settings.
Show Answer
AWS Resources:
- CodeStar Connection — Connects AWS to GitHub org
- Complete handshake in console after creating via CLI
- CodeBuild Project:
my-app-pr-check— For pull requests- Source: GitHub with webhook
- Webhook filter:
EVENT=PULL_REQUEST_CREATED,PULL_REQUEST_UPDATED reportBuildStatus=true(shows ✅/❌ on PRs)- Artefacts: None (test only)
- CodeBuild Project:
my-app-build— For pipeline builds- Source: CodePipeline (receives artefact from Source stage)
- Builds Docker image, pushes to ECR
- Outputs deployment artefact (appspec.yml + scripts)
- CodePipeline:
my-app-pipeline- Source stage:
CodeStarSourceConnection→ GitHubmainbranch,DetectChanges=true - Build stage:
my-app-buildCodeBuild project - Deploy-Staging stage: CodeDeploy to staging deployment group
- Approval stage: Manual approval with SNS notification
- Deploy-Production stage: CodeDeploy to production deployment group
- Source stage:
- CodeDeploy Application:
my-app- Staging deployment group: In-place,
HalfAtATime - Production deployment group: Blue/Green with ALB traffic shift
- Auto-rollback on failure and CloudWatch alarms
- Staging deployment group: In-place,
- Supporting resources:
- S3 bucket for pipeline artefacts
- ECR repository for Docker images
- ALB with two target groups (Blue + Green)
- SNS topic for approval notifications
- IAM roles: CodePipeline, CodeBuild, CodeDeploy, EC2 instance profile
Next Chapter → Containers