Skip to content

AWS CodePipeline & CodeBuild

Chapter 32: AWS CodePipeline & CI/CD Best Practices

Section titled “Chapter 32: AWS CodePipeline & CI/CD Best Practices”

AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates.

AWS CodePipeline Overview
+------------------------------------------------------------------+
| |
| +------------------------+ |
| | AWS CodePipeline | |
| +------------------------+ |
| | |
| +---------------------+---------------------+ |
| | | | |
| v v v |
| +----------+ +----------+ +----------+ |
| | Stages | | Actions | | Transitions |
| | | | | | | |
| | - Source | | - Source | | - Manual | |
| | - Build | | - Build | | - Auto | |
| | - Deploy | | - Deploy | | - Conditions |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
CodePipeline Structure
+------------------------------------------------------------------+
| |
| Pipeline |
| +------------------------------------------------------------+ |
| | | |
| | Stage 1: Source Stage 2: Build Stage 3: Deploy |
| | +----------------+ +----------------+ +----------------+ |
| | | Action 1 | --> | Action 1 | --> | Action 1 | |
| | | (CodeCommit) | | (CodeBuild) | | (CodeDeploy) | |
| | +----------------+ +----------------+ +----------------+ |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

CodePipeline Stages
+------------------------------------------------------------------+
| |
| Source Stage |
| +------------------------------------------------------------+ |
| | | |
| | Supported Sources: | |
| | +------------------------------------------------------+ | |
| | | - AWS CodeCommit | | |
| | | - GitHub | | |
| | | - Bitbucket | | |
| | | - Amazon S3 | | |
| | | - Amazon ECR | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Build Stage |
| +------------------------------------------------------------+ |
| | | |
| | Supported Build Providers: | |
| | +------------------------------------------------------+ | |
| | | - AWS CodeBuild | | |
| | | - Jenkins | | |
| | | - TeamCity | | |
| | | - CloudBees | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
| Deploy Stage |
| +------------------------------------------------------------+ |
| | | |
| | Supported Deploy Providers: | |
| | +------------------------------------------------------+ | |
| | | - AWS CodeDeploy (EC2, Lambda, ECS) | | |
| | | - AWS CloudFormation | | |
| | | - AWS Elastic Beanstalk | | |
| | | - AWS ECS | | |
| | | - AWS S3 | | |
| | | - AWS AppConfig | | |
| | +------------------------------------------------------+ | |
| | | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CodePipeline Action Types
+------------------------------------------------------------------+
| |
| Source Actions |
| +------------------------------------------------------------+ |
| | - Checkout code from repository | |
| | - Trigger pipeline on changes | |
| | - Output: source artifacts | |
| +------------------------------------------------------------+ |
| |
| Build Actions |
| +------------------------------------------------------------+ |
| | - Compile source code | |
| | - Run tests | |
| | - Output: build artifacts | |
| +------------------------------------------------------------+ |
| |
| Deploy Actions |
| +------------------------------------------------------------+ |
| | - Deploy to target environment | |
| | - Update infrastructure | |
| | - Output: deployment status | |
| +------------------------------------------------------------+ |
| |
| Approval Actions |
| +------------------------------------------------------------+ |
| | - Manual approval gate | |
| | - Require human review | |
| | - Output: approval status | |
| +------------------------------------------------------------+ |
| |
| Test Actions |
| +------------------------------------------------------------+ |
| | - Run automated tests | |
| | - Integration tests | |
| | - Output: test results | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

CodePipeline Definition
+------------------------------------------------------------------+
| |
| { |
| "pipeline": { |
| "name": "MyAppPipeline", |
| "roleArn": "arn:aws:iam::123456789012:role/CodePipelineRole", |
| "artifactStore": { |
| "type": "S3", |
| "location": "my-pipeline-artifacts" |
| }, |
| "stages": [ |
| { |
| "name": "Source", |
| "actions": [ |
| { |
| "name": "SourceAction", |
| "actionTypeId": { |
| "category": "Source", |
| "owner": "AWS", |
| "provider": "CodeCommit", |
| "version": "1" |
| }, |
| "outputArtifacts": [ |
| { "name": "SourceOutput" } |
| ], |
| "configuration": { |
| "RepositoryName": "my-repo", |
| "BranchName": "main" |
| } |
| } |
| ] |
| }, |
| { |
| "name": "Build", |
| "actions": [ |
| { |
| "name": "BuildAction", |
| "actionTypeId": { |
| "category": "Build", |
| "owner": "AWS", |
| "provider": "CodeBuild", |
| "version": "1" |
| }, |
| "inputArtifacts": [ |
| { "name": "SourceOutput" } |
| ], |
| "outputArtifacts": [ |
| { "name": "BuildOutput" } |
| ], |
| "configuration": { |
| "ProjectName": "my-build-project" |
| } |
| } |
| ] |
| }, |
| { |
| "name": "Deploy", |
| "actions": [ |
| { |
| "name": "DeployAction", |
| "actionTypeId": { |
| "category": "Deploy", |
| "owner": "AWS", |
| "provider": "CodeDeploy", |
| "version": "1" |
| }, |
| "inputArtifacts": [ |
| { "name": "BuildOutput" } |
| ], |
| "configuration": { |
| "ApplicationName": "my-app", |
| "DeploymentGroupName": "my-deployment-group" |
| } |
| } |
| ] |
| } |
| ] |
| } |
| } |
| |
+------------------------------------------------------------------+

Multi-Stage Pipeline Pattern
+------------------------------------------------------------------+
| |
| +----------+ +----------+ +----------+ +----------+ |
| | Source | -->| Build | -->| Test | -->| Approval | |
| | | | | | | | | |
| | CodeCommit| | CodeBuild| | CodeBuild| | Manual | |
| +----------+ +----------+ +----------+ +----------+ |
| | |
| v |
| +----------+ +----------+ +----------+ |
| | Prod | <--| Staging | <--| Deploy | |
| | Deploy | | Deploy | | Dev | |
| | | | | | | |
| | CodeDeploy| | CodeDeploy| | CodeDeploy| |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
Blue/Green Deployment Pipeline
+------------------------------------------------------------------+
| |
| +----------+ +----------+ +----------+ +----------+ |
| | Source | -->| Build | -->| Test | -->| Create | |
| | | | | | | | Green | |
| +----------+ +----------+ +----------+ +----------+ |
| | |
| v |
| +----------+ +----------+ +----------+ |
| | Terminate| <--| Shift | <--| Validate | |
| | Blue | | Traffic | | Green | |
| +----------+ +----------+ +----------+ |
| |
+------------------------------------------------------------------+
Cross-Account Pipeline
+------------------------------------------------------------------+
| |
| Dev Account Prod Account |
| +------------------+ +------------------+ |
| | | | | |
| | +----------+ | | +----------+ | |
| | | Source | | | | Deploy | | |
| | | (CodeCommit) | | | (CodeDeploy) | |
| | +----------+ | | +----------+ | |
| | | | | ^ | |
| | v | | | | |
| | +----------+ | | | | |
| | | Build | | | | | |
| | | (CodeBuild) | | | | |
| | +----------+ | | | | |
| | | | | | | |
| | +---------+------------>+--------+ | |
| | | | | |
| +------------------+ +------------------+ |
| |
+------------------------------------------------------------------+

Terminal window
# Create pipeline
aws codepipeline create-pipeline \
--cli-input-json file://pipeline-definition.json
# Get pipeline
aws codepipeline get-pipeline \
--name my-pipeline
# List pipelines
aws codepipeline list-pipelines
# Update pipeline
aws codepipeline update-pipeline \
--cli-input-json file://pipeline-definition.json
# Start pipeline execution
aws codepipeline start-pipeline-execution \
--name my-pipeline
# List pipeline executions
aws codepipeline list-pipeline-executions \
--pipeline-name my-pipeline
# Get pipeline execution
aws codepipeline get-pipeline-execution \
--pipeline-name my-pipeline \
--pipeline-execution-id execution-id
# Retry stage execution
aws codepipeline retry-stage-execution \
--pipeline-name my-pipeline \
--stage-name Build \
--pipeline-execution-id execution-id \
--retry-mode FAILED_ACTIONS
# Stop pipeline execution
aws codepipeline stop-pipeline-execution \
--pipeline-name my-pipeline \
--pipeline-execution-id execution-id
# Put approval result
aws codepipeline put-approval-result \
--pipeline-name my-pipeline \
--stage-name Approval \
--action-name ApprovalAction \
--result summary="Approved",status=Approved \
--token approval-token
# Delete pipeline
aws codepipeline delete-pipeline \
--name my-pipeline

Pipeline Design Best Practices
+------------------------------------------------------------------+
| |
| 1. Keep pipelines simple and focused |
| +------------------------------------------------------------+ |
| | - Single responsibility per pipeline | |
| | - Separate pipelines for different environments | |
| +------------------------------------------------------------+ |
| |
| 2. Implement proper artifact management |
| +------------------------------------------------------------+ |
| | - Use versioned artifacts | |
| | - Store artifacts in S3 with lifecycle policies | |
| +------------------------------------------------------------+ |
| |
| 3. Use manual approvals for production |
| +------------------------------------------------------------+ |
| | - Require human approval before production deployment | |
| | - Include relevant stakeholders | |
| +------------------------------------------------------------+ |
| |
| 4. Implement automated testing |
| +------------------------------------------------------------+ |
| | - Unit tests in build stage | |
| | - Integration tests in test stage | |
| | - Security scanning | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CI/CD Security Best Practices
+------------------------------------------------------------------+
| |
| 1. Use least privilege IAM roles |
| +------------------------------------------------------------+ |
| | - Separate roles for each service | |
| | - Use resource-level permissions | |
| +------------------------------------------------------------+ |
| |
| 2. Secure secrets management |
| +------------------------------------------------------------+ |
| | - Use Secrets Manager or Parameter Store | |
| | - Never store secrets in code | |
| | - Rotate secrets regularly | |
| +------------------------------------------------------------+ |
| |
| 3. Enable encryption |
| +------------------------------------------------------------+ |
| | - Encrypt artifacts at rest | |
| | - Use KMS for encryption keys | |
| +------------------------------------------------------------+ |
| |
| 4. Implement audit logging |
| +------------------------------------------------------------+ |
| | - Enable CloudTrail for all CI/CD services | |
| | - Monitor pipeline execution logs | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
CI/CD Performance Best Practices
+------------------------------------------------------------------+
| |
| 1. Optimize build time |
| +------------------------------------------------------------+ |
| | - Use caching for dependencies | |
| | - Parallelize tests | |
| | - Use appropriate compute types | |
| +------------------------------------------------------------+ |
| |
| 2. Minimize artifact size |
| +------------------------------------------------------------+ |
| | - Only include necessary files | |
| | - Use .gitignore and exclude patterns | |
| +------------------------------------------------------------+ |
| |
| 3. Use incremental deployments |
| +------------------------------------------------------------+ |
| | - Deploy only changed components | |
| | - Use rolling updates | |
| +------------------------------------------------------------+ |
| |
| 4. Implement proper monitoring |
| +------------------------------------------------------------+ |
| | - Set up CloudWatch alarms | |
| | - Monitor pipeline execution duration | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

EventBridge Integration
+------------------------------------------------------------------+
| |
| CodeCommit Event --> EventBridge --> CodePipeline |
| |
| Event Pattern: |
| +------------------------------------------------------------+ |
| | { | |
| | "source": ["aws.codecommit"], | |
| | "detail-type": ["CodeCommit Repository State Change"], | |
| | "detail": { | |
| | "event": ["push"], | |
| | "repositoryName": ["my-repo"], | |
| | "referenceName": ["main"] | |
| | } | |
| | } | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
SNS Notification Integration
+------------------------------------------------------------------+
| |
| Pipeline Events --> SNS Topic --> Subscribers |
| |
| Notification Types: |
| +------------------------------------------------------------+ |
| | - Pipeline execution started | |
| | - Pipeline execution succeeded | |
| | - Pipeline execution failed | |
| | - Stage execution started/succeeded/failed | |
| | - Action execution started/succeeded/failed | |
| | - Manual approval needed | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Common CodePipeline Issues
+------------------------------------------------------------------+
| |
| Issue 1: Pipeline stuck in progress |
| +------------------------------------------------------------+ |
| | Cause: Action timeout or resource unavailable | |
| | Solution: Check action logs, increase timeout | |
| +------------------------------------------------------------+ |
| |
| Issue 2: Artifact not found |
| +------------------------------------------------------------+ |
| | Cause: Incorrect artifact name or missing output | |
| | Solution: Verify artifact names match between stages | |
| +------------------------------------------------------------+ |
| |
| Issue 3: Permission denied |
| +------------------------------------------------------------+ |
| | Cause: Missing IAM permissions | |
| | Solution: Check service role permissions | |
| +------------------------------------------------------------+ |
| |
| Issue 4: Source change not triggering pipeline |
| +------------------------------------------------------------+ |
| | Cause: Webhook not configured or disabled | |
| | Solution: Check CloudWatch Events rule for source | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

CodePipeline is the orchestration engine for AWS CI/CD. Understanding its patterns is critical for SREs managing deployment workflows and reliability.

CodePipeline in DevOps/SRE
+------------------------------------------------------------------+
| |
| SRE Pipeline Principles: |
| |
| 1. Deployment Safety & Reliability |
| +----------------------------------------------------------+ |
| | - Pipeline as code: version-controlled deployment | |
| | - Automated gates: tests, security scans, approvals | |
| | - Blue-green/canary for zero-downtime releases | |
| +----------------------------------------------------------+ |
| |
| 2. Observability Integration |
| +----------------------------------------------------------+ |
| | - Pipeline emits CloudWatch metrics on success/failure | |
| | - SNS notifications for on-call alerts | |
| | - X-Ray tracing for distributed deployment analysis | |
| +----------------------------------------------------------+ |
| |
| 3. Error Budget & Release Engineering |
| +----------------------------------------------------------+ |
| | - Pipeline respects error budget policies | |
| | - Auto-rollback on SLO violations | |
| | - Feature flags for gradual rollouts | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Terminal window
# Install AWS CLI and tools on Arch Linux
sudo pacman -S aws-cli-v2 jq docker git
yay -S aws-copilot
# Pipeline monitoring script
#!/bin/bash
# ~/bin/pipeline-monitor.sh
set -euo pipefail
PIPELINE_NAME="${1:-my-pipeline}"
echo "=== Pipeline: $PIPELINE_NAME ==="
aws codepipeline get-pipeline-state \
--name "$PIPELINE_NAME" \
--query 'stageStates[].{Stage:stageName,Status:latestExecution.status}' \
--output table
echo ""
echo "=== Latest Execution ==="
aws codepipeline list-pipeline-executions \
--pipeline-name "$PIPELINE_NAME" \
--max-results 1 \
--query 'pipelineExecutionSummaries[0].{Status:status,Start:startTime,LastUpdate:lastUpdateTime}' \
--output table
# Watch pipeline progress
watch -n 10 'aws codepipeline get-pipeline-state --name my-pipeline --query "stageStates[].{Stage:stageName,Status:latestExecution.status}" --output table'

CodePipeline Anti-Patterns
+------------------------------------------------------------------+
| |
| ❌ Mistake 1: No Cross-Region Replication |
| +----------------------------------------------------------+ |
| | Problem: Pipeline in one region, apps in another | |
| | Impact: Latency, data sovereignty issues, failover gaps | |
| | Fix: Deploy pipelines in each region or use cross-region| |
| +----------------------------------------------------------+ |
| |
| ❌ Mistake 2: Hardcoded Artifact S3 Buckets |
| +----------------------------------------------------------+ |
| | Problem: Bucket names embedded in pipeline config | |
| | Impact: Cross-account deployments fail | |
| | Fix: Use pipeline variables and cross-account IAM | |
| +----------------------------------------------------------+ |
| |
| ❌ Mistake 3: No Manual Approvals for Production |
| +----------------------------------------------------------+ |
| | Problem: Auto-deploy to production on every commit | |
| | Impact: Unreviewed changes reach production | |
| | Fix: Add approval actions before production stages | |
| +----------------------------------------------------------+ |
| |
| ❌ Mistake 4: Ignoring Pipeline Execution History |
| +----------------------------------------------------------+ |
| | Problem: No logging of who approved what and when | |
| | Impact: Audit failures, incident investigation delays | |
| | Fix: Enable CloudTrail for pipeline events | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

  1. Q: How does CodePipeline integrate with CodeBuild and CodeDeploy?

    • A: CodePipeline orchestrates the workflow: source stage pulls code → CodeBuild compiles/tests → CodeDeploy deploys to compute services. Artifacts are stored in S3 between stages. Pipeline can trigger on source changes (CodeCommit, S3, GitHub) and invoke Lambda for custom actions.
  2. Q: Explain the difference between sequential and parallel actions in CodePipeline.

    • A: Sequential actions run one after another within a stage (e.g., build then test). Parallel actions run simultaneously (e.g., multiple test suites). Parallel actions reduce execution time but require careful dependency management. Transitions between stages can be conditional based on action results.
  1. Q: Design a multi-region deployment pipeline.
    • A: Use CodePipeline with cross-region actions: source in primary region → build in primary → deploy to staging → manual approval → parallel deploy to multiple regions using CodeDeploy. Each region has its own pipeline or uses StackSets for infrastructure. Enable pipeline CloudTrail for audit. Use Route53 weighted routing for traffic shifting.

Exam Tip

Key Exam Points
+------------------------------------------------------------------+
| |
| 1. CodePipeline orchestrates the entire CI/CD workflow |
| |
| 2. Pipelines consist of stages, actions, and transitions |
| |
| 3. Each stage can have multiple actions (sequential/parallel) |
| |
| 4. Artifacts are passed between stages via S3 |
| |
| 5. Manual approval actions require human intervention |
| |
| 6. EventBridge triggers pipelines on source changes |
| |
| 7. Cross-account pipelines use KMS encryption |
| |
| 8. CodePipeline supports third-party integrations |
| |
| 9. Use retry-stage-execution to retry failed actions |
| |
| 10. Pipeline execution can be stopped mid-execution |
| |
+------------------------------------------------------------------+

Chapter 32 Summary
+------------------------------------------------------------------+
| |
| AWS CodePipeline |
| +------------------------------------------------------------+ |
| | - Fully managed continuous delivery service | |
| | - Visual workflow for release process | |
| | - Integrates with AWS and third-party services | |
| +------------------------------------------------------------+ |
| |
| Pipeline Components |
| +------------------------------------------------------------+ |
| | - Stages: Source, Build, Test, Deploy, Approval | |
| | - Actions: Individual operations within stages | |
| | - Artifacts: Files passed between stages | |
| | - Transitions: Movement between stages | |
| +------------------------------------------------------------+ |
| |
| Best Practices |
| +------------------------------------------------------------+ |
| | - Implement automated testing | |
| | - Use manual approvals for production | |
| | - Secure secrets properly | |
| | - Monitor pipeline performance | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

Previous Chapter: Chapter 31: AWS CodeCommit, CodeBuild & CodeDeploy Next Chapter: Chapter 33: AWS CloudFormation - Infrastructure as Code