Skip to content

Pipeline

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 | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

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