AWS CodePipeline CI/CD
When to Use
- Building CI/CD pipelines with AWS CodePipeline, CodeBuild, and CodeDeploy
- Choosing between deployment strategies (In-Place, Blue/Green, Canary, Linear)
- Configuring CodeBuild buildspec.yml phases and artifacts
- Implementing approval gates and notifications in pipelines
- Managing package dependencies with CodeArtifact
- Preparing for AWS DOP-C02 or DVA-C02 exams
Core Jobs
1. Pipeline Architecture
Standard pipeline flow:
Source → Build → Test → [Approval] → Deploy (Staging) → [Approval] → Deploy (Production)
Pipeline stages and actions:
| Stage |
Services |
Purpose |
| Source |
CodeCommit, S3, GitHub, ECR |
Trigger on code/artifact change |
| Build |
CodeBuild |
Compile, test, package artifacts |
| Test |
CodeBuild, Lambda |
Integration/load tests |
| Deploy |
CodeDeploy, ECS, CloudFormation, S3, Elastic Beanstalk |
Deploy to target |
| Approval |
Manual approval action |
Human gate with SNS notification |
Action constraints:
- Actions in the same stage run in parallel by default
- Use
runOrder to sequence actions within a stage (runOrder 1 before runOrder 2)
- Cross-region actions: replicate artifacts to target region's S3 first
2. CodeBuild — buildspec.yml
version: 0.2
env:
variables:
ENV: production
parameter-store:
DB_PASSWORD: /myapp/db/password # from SSM Parameter Store
secrets-manager:
API_KEY: myapp/api-key # from Secrets Manager
phases:
install:
runtime-versions:
nodejs: 18
commands:
- npm install
pre_build:
commands:
- echo "Running pre-build checks..."
- aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REGISTRY
build:
commands:
- npm run build
- npm run test
- docker build -t $IMAGE_TAG .
post_build:
commands:
- docker push $IMAGE_TAG
- echo "Build completed"
artifacts:
files:
- '**/*'
base-directory: dist
discard-paths: no
cache:
paths:
- node_modules/**/* # cache for faster builds
Key sections:
install: Install runtime and tools
pre_build: Authentication, setup
build: Main compilation and testing
post_build: Push images, notifications
artifacts: Files passed to next pipeline stage
cache: S3-backed cache between builds (speeds up dependency downloads)
3. CodeDeploy Deployment Strategies
For EC2 / On-premises:
| Strategy |
Description |
Downtime |
Rollback |
| In-Place (Rolling) |
Deploy to existing instances; configurable batch size |
Brief (batch update) |
Redeploy old version |
| Blue/Green |
Create new ASG with new version; shift traffic via ELB |
Zero downtime |
Keep old ASG, redirect traffic back |
In-Place minimum health: MinimumHealthyHosts (percentage or count that must remain healthy during deployment).
For AWS Lambda:
| Strategy |
Behavior |
| AllAtOnce |
Immediately shift 100% traffic to new version |
| Canary10Percent5Minutes |
Shift 10%, wait 5 min, shift remaining 90% |
| Linear10PercentEvery1Minute |
Shift 10% every 1 minute (10 steps = 100% in 10 min) |
| Linear10PercentEvery3Minutes |
Shift 10% every 3 min (30 min total) |
For ECS (Blue/Green):
- CodeDeploy manages ELB target group switching
- New task set (green) created alongside original (blue)
- Traffic shifted gradually using same Lambda strategies (Canary, Linear)
- Rollback: immediate traffic shift back to original task set
4. CodeDeploy appspec.yml
For EC2:
version: 0.0
os: linux
files:
- source: /src
destination: /var/www/html
hooks:
BeforeInstall:
- location: scripts/stop_server.sh
timeout: 60
AfterInstall:
- location: scripts/install_dependencies.sh
ApplicationStart:
- location: scripts/start_server.sh
ValidateService:
- location: scripts/validate.sh
timeout: 60
For Lambda:
version: 0.0
Resources:
- MyFunction:
Type: AWS::Lambda::Function
Properties:
Name: MyFunction
Alias: live
CurrentVersion: !Ref LambdaVersion
TargetVersion: !Ref NewLambdaVersion
Hooks:
BeforeAllowTraffic: PreTrafficHook # Lambda to run before traffic shift
AfterAllowTraffic: PostTrafficHook # Lambda to run after traffic shift
Hook lifecycle order (EC2):
ApplicationStop → DownloadBundle → BeforeInstall → Install → AfterInstall → ApplicationStart → ValidateService
5. Triggering Pipelines
| Source event |
Pipeline trigger |
| CodeCommit push |
EventBridge rule (automatic) |
| S3 object change |
EventBridge rule |
| GitHub/Bitbucket push |
CodeStar Connection webhook |
| ECR image push |
EventBridge rule → pipeline (not direct) |
| Scheduled |
EventBridge schedule → pipeline |
| Manual |
Console, CLI, SDK |
ECR push → Pipeline: CodePipeline cannot watch ECR directly. Use EventBridge rule:
source: aws.ecr → detail-type: "ECR Image Action" → Pipeline trigger.
6. CodeArtifact
- Managed artifact repository: npm, Maven, PyPI, NuGet, RubyGems, Swift
- Upstream repositories: proxy public repos (npmjs.com, PyPI) with caching
- Domain: grouping of repositories; single KMS key for all repos in domain
- Cross-account: share repositories across accounts via resource policy
- Integration: CodeBuild pulls from CodeArtifact (private mirror); reduces public registry dependencies
Key Concepts
- Artifact — file(s) passed between pipeline stages (S3-backed); CodePipeline manages lifecycle
- Rollback — CodeDeploy monitors CloudWatch alarms; triggers rollback if alarms fire during deployment
- Deployment configuration — named set of deployment rules (health check %, traffic shift strategy)
- Deployment group — set of target instances/functions for a deployment (tags, ASG, ECS service)
- CloudFormation + CodePipeline — CREATE_UPDATE action in pipeline for IaC deployments; supports change sets (review before apply)
- Elastic Beanstalk — PaaS that CodePipeline can deploy to directly; handles Blue/Green via environment swaps
- CodeGuru Reviewer — ML-powered code review; integrates with CodePipeline for automated review gates
Checklist
Output Format
- 🔴 Critical — no approval gates before production; no rollback configured for CodeDeploy (manual recovery only); CodeBuild retrieving secrets via env vars in plain text (use Secrets Manager integration)
- 🟡 Warning — In-Place deployment on production (downtime risk); ECR source not via EventBridge (pipeline won't trigger on image push); no CodeBuild cache (slow builds)
- 🟢 Suggestion — Lambda Linear traffic shifting for safer production deploys; CodeArtifact for dependency control; CodeGuru Reviewer for automated code quality gates
Exam Tips
- CodeDeploy appspec.yml = deployment instructions and lifecycle hook scripts; file required for all CodeDeploy deployments
- Blue/Green EC2 = new ASG created; traffic shifted via ELB; old ASG kept for configurable period for rollback
- Lambda Linear10PercentEvery1Minute = 10% traffic shifted every minute (10 steps to reach 100% in 10 minutes)
- CodePipeline cannot trigger from ECR push directly — use EventBridge rule (
aws.ecr image action event) → trigger pipeline
- buildspec.yml artifacts section = files passed to next pipeline stage (stored in S3); missing this = next stage has no input
- CodeDeploy minimum health (
MinimumHealthyHosts) = must keep X% of instances healthy during rolling deploy to production
- Canary deployment: 10% of traffic for validation period, then shift remaining 90% — fastest rollback if canary fails
- appspec.yml hooks execute scripts at each lifecycle event; hook timeouts default 3600s; best practice validate with
ValidateService hook
1---2name: codepipeline-cicd3description: Use when building AWS CI/CD pipelines with CodePipeline/CodeBuild/CodeDeploy, choosing deployment strategies, configuring buildspec.yml, or setting up artifact management with CodeArtifact. Covers AWS DOP-C02 and DVA-C02 CI/CD domains.4---56# AWS CodePipeline CI/CD78## When to Use9- Building CI/CD pipelines with AWS CodePipeline, CodeBuild, and CodeDeploy10- Choosing between deployment strategies (In-Place, Blue/Green, Canary, Linear)11- Configuring CodeBuild buildspec.yml phases and artifacts12- Implementing approval gates and notifications in pipelines13- Managing package dependencies with CodeArtifact14- Preparing for AWS DOP-C02 or DVA-C02 exams1516## Core Jobs1718### 1. Pipeline Architecture1920**Standard pipeline flow**:21```22Source → Build → Test → [Approval] → Deploy (Staging) → [Approval] → Deploy (Production)23```2425**Pipeline stages and actions**:2627| Stage | Services | Purpose |28|-------|---------|---------|29| **Source** | CodeCommit, S3, GitHub, ECR | Trigger on code/artifact change |30| **Build** | CodeBuild | Compile, test, package artifacts |31| **Test** | CodeBuild, Lambda | Integration/load tests |32| **Deploy** | CodeDeploy, ECS, CloudFormation, S3, Elastic Beanstalk | Deploy to target |33| **Approval** | Manual approval action | Human gate with SNS notification |3435**Action constraints**:36- Actions in the same stage run in parallel by default37- Use `runOrder` to sequence actions within a stage (runOrder 1 before runOrder 2)38- Cross-region actions: replicate artifacts to target region's S3 first3940### 2. CodeBuild — buildspec.yml4142```yaml43version: 0.24445env:46 variables:47 ENV: production48 parameter-store:49 DB_PASSWORD: /myapp/db/password # from SSM Parameter Store50 secrets-manager:51 API_KEY: myapp/api-key # from Secrets Manager5253phases:54 install:55 runtime-versions:56 nodejs: 1857 commands:58 - npm install5960 pre_build:61 commands:62 - echo "Running pre-build checks..."63 - aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REGISTRY6465 build:66 commands:67 - npm run build68 - npm run test69 - docker build -t $IMAGE_TAG .7071 post_build:72 commands:73 - docker push $IMAGE_TAG74 - echo "Build completed"7576artifacts:77 files:78 - '**/*'79 base-directory: dist80 discard-paths: no8182cache:83 paths:84 - node_modules/**/* # cache for faster builds85```8687**Key sections**:88- `install`: Install runtime and tools89- `pre_build`: Authentication, setup90- `build`: Main compilation and testing91- `post_build`: Push images, notifications92- `artifacts`: Files passed to next pipeline stage93- `cache`: S3-backed cache between builds (speeds up dependency downloads)9495### 3. CodeDeploy Deployment Strategies9697**For EC2 / On-premises**:9899| Strategy | Description | Downtime | Rollback |100|----------|------------|---------|---------|101| **In-Place (Rolling)** | Deploy to existing instances; configurable batch size | Brief (batch update) | Redeploy old version |102| **Blue/Green** | Create new ASG with new version; shift traffic via ELB | Zero downtime | Keep old ASG, redirect traffic back |103104In-Place minimum health: `MinimumHealthyHosts` (percentage or count that must remain healthy during deployment).105106**For AWS Lambda**:107108| Strategy | Behavior |109|----------|---------|110| **AllAtOnce** | Immediately shift 100% traffic to new version |111| **Canary10Percent5Minutes** | Shift 10%, wait 5 min, shift remaining 90% |112| **Linear10PercentEvery1Minute** | Shift 10% every 1 minute (10 steps = 100% in 10 min) |113| **Linear10PercentEvery3Minutes** | Shift 10% every 3 min (30 min total) |114115**For ECS (Blue/Green)**:116- CodeDeploy manages ELB target group switching117- New task set (green) created alongside original (blue)118- Traffic shifted gradually using same Lambda strategies (Canary, Linear)119- Rollback: immediate traffic shift back to original task set120121### 4. CodeDeploy appspec.yml122123**For EC2**:124```yaml125version: 0.0126os: linux127files:128 - source: /src129 destination: /var/www/html130hooks:131 BeforeInstall:132 - location: scripts/stop_server.sh133 timeout: 60134 AfterInstall:135 - location: scripts/install_dependencies.sh136 ApplicationStart:137 - location: scripts/start_server.sh138 ValidateService:139 - location: scripts/validate.sh140 timeout: 60141```142143**For Lambda**:144```yaml145version: 0.0146Resources:147 - MyFunction:148 Type: AWS::Lambda::Function149 Properties:150 Name: MyFunction151 Alias: live152 CurrentVersion: !Ref LambdaVersion153 TargetVersion: !Ref NewLambdaVersion154Hooks:155 BeforeAllowTraffic: PreTrafficHook # Lambda to run before traffic shift156 AfterAllowTraffic: PostTrafficHook # Lambda to run after traffic shift157```158159**Hook lifecycle order** (EC2):160ApplicationStop → DownloadBundle → BeforeInstall → Install → AfterInstall → ApplicationStart → ValidateService161162### 5. Triggering Pipelines163164| Source event | Pipeline trigger |165|-------------|----------------|166| CodeCommit push | EventBridge rule (automatic) |167| S3 object change | EventBridge rule |168| GitHub/Bitbucket push | CodeStar Connection webhook |169| ECR image push | **EventBridge rule** → pipeline (not direct) |170| Scheduled | EventBridge schedule → pipeline |171| Manual | Console, CLI, SDK |172173**ECR push → Pipeline**: CodePipeline cannot watch ECR directly. Use EventBridge rule:174`source: aws.ecr` → `detail-type: "ECR Image Action"` → Pipeline trigger.175176### 6. CodeArtifact177178- Managed artifact repository: npm, Maven, PyPI, NuGet, RubyGems, Swift179- Upstream repositories: proxy public repos (npmjs.com, PyPI) with caching180- Domain: grouping of repositories; single KMS key for all repos in domain181- Cross-account: share repositories across accounts via resource policy182- Integration: CodeBuild pulls from CodeArtifact (private mirror); reduces public registry dependencies183184## Key Concepts185186- **Artifact** — file(s) passed between pipeline stages (S3-backed); CodePipeline manages lifecycle187- **Rollback** — CodeDeploy monitors CloudWatch alarms; triggers rollback if alarms fire during deployment188- **Deployment configuration** — named set of deployment rules (health check %, traffic shift strategy)189- **Deployment group** — set of target instances/functions for a deployment (tags, ASG, ECS service)190- **CloudFormation + CodePipeline** — CREATE_UPDATE action in pipeline for IaC deployments; supports change sets (review before apply)191- **Elastic Beanstalk** — PaaS that CodePipeline can deploy to directly; handles Blue/Green via environment swaps192- **CodeGuru Reviewer** — ML-powered code review; integrates with CodePipeline for automated review gates193194## Checklist195196- [ ] Manual approval gates before production deployments?197- [ ] CodeDeploy deployment alarms configured (CloudWatch → auto rollback on alarm)?198- [ ] Blue/Green deployment for zero-downtime Lambda and ECS deploys?199- [ ] buildspec.yml artifact section correctly defined for pipeline stage handoff?200- [ ] CodeBuild cache configured for dependency directories (node_modules, .m2, pip)?201- [ ] ECR push pipeline trigger via EventBridge rule (not direct ECR source)?202- [ ] CodeArtifact used as private mirror for external packages (security + availability)?203- [ ] IAM roles for CodePipeline/CodeBuild follow least-privilege principle?204205## Output Format206207- 🔴 **Critical** — no approval gates before production; no rollback configured for CodeDeploy (manual recovery only); CodeBuild retrieving secrets via env vars in plain text (use Secrets Manager integration)208- 🟡 **Warning** — In-Place deployment on production (downtime risk); ECR source not via EventBridge (pipeline won't trigger on image push); no CodeBuild cache (slow builds)209- 🟢 **Suggestion** — Lambda Linear traffic shifting for safer production deploys; CodeArtifact for dependency control; CodeGuru Reviewer for automated code quality gates210211## Exam Tips212213- **CodeDeploy appspec.yml** = deployment instructions and lifecycle hook scripts; file required for all CodeDeploy deployments214- **Blue/Green EC2** = new ASG created; traffic shifted via ELB; old ASG kept for configurable period for rollback215- **Lambda Linear10PercentEvery1Minute** = 10% traffic shifted every minute (10 steps to reach 100% in 10 minutes)216- **CodePipeline cannot trigger from ECR push directly** — use EventBridge rule (`aws.ecr` image action event) → trigger pipeline217- **buildspec.yml artifacts section** = files passed to next pipeline stage (stored in S3); missing this = next stage has no input218- **CodeDeploy minimum health** (`MinimumHealthyHosts`) = must keep X% of instances healthy during rolling deploy to production219- **Canary deployment**: 10% of traffic for validation period, then shift remaining 90% — fastest rollback if canary fails220- **appspec.yml hooks** execute scripts at each lifecycle event; hook timeouts default 3600s; best practice validate with `ValidateService` hook