CodePipeline and Testing Strategies
Overview
AWS CodePipeline orchestrates the release workflow. A pipeline is a series of stages (Source, Build, Test, Deploy), and each stage contains actions that run sequentially or in parallel. Artifacts move between actions, and the pipeline stops at the first failure so bad changes never reach production.
Picture an airport security line: baggage passes checkpoints in order, and any alarm halts the belt. Each checkpoint is a test, scan or approval; quality gates in the pipeline are the same idea, giving confidence that anything that reaches production has passed everything cheaper to catch earlier.
DOP-C02 tests pipeline structure, cross-account and cross-Region patterns, manual approvals, invoking Lambda or Step Functions, pipeline notifications and where each type of test belongs.
Key Concepts
- A pipeline has stages, stages contain actions; actions in the same runOrder run in parallel, higher runOrder runs after lower.
- Action categories: Source, Build, Test, Deploy, Approval, Invoke; providers include CodeBuild, CodeDeploy, CloudFormation, ECS, S3, Lambda, Step Functions, Beanstalk and Service Catalog.
- Manual approval actions pause the pipeline and publish to SNS; the approver needs
codepipeline:PutApprovalResult. - Cross-account deployments assume a role in the target account; artifacts are decrypted with a shared CMK.
- Cross-Region actions need an artifact bucket in each Region (CodePipeline replicates artifacts for you).
- EventBridge emits pipeline, stage and action state-change events for notifications and automation; notification rules integrate with SNS and chat.
- Variables (namespaces) pass values such as commit ID between actions; V2 adds pipeline variables and stage rollback and conditions.
- Place fast, cheap tests first (unit, lint, SAST), then integration, then slower end-to-end and load tests before production.
Detailed Notes
Pipeline anatomy
Resources:
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
PipelineType: V2
ExecutionMode: QUEUED
RoleArn: !GetAtt PipelineRole.Arn
ArtifactStore: { Type: S3, Location: !Ref ArtifactBucket }
Stages:
- Name: Source
Actions: [ ... ]
- Name: Build
Actions:
- Name: Build
ActionTypeId: { Category: Build, Owner: AWS, Provider: CodeBuild, Version: "1" }
InputArtifacts: [{ Name: SourceOut }]
OutputArtifacts: [{ Name: BuildOut }]
Configuration: { ProjectName: !Ref BuildProject }
- Name: Staging
Actions:
- Name: Deploy
RunOrder: 1
ActionTypeId: { Category: Deploy, Owner: AWS, Provider: CloudFormation, Version: "1" }
Configuration:
ActionMode: CREATE_UPDATE
StackName: orders-staging
TemplatePath: BuildOut::template.yaml
RoleArn: !GetAtt CfnRole.Arn
Capabilities: CAPABILITY_IAM
- Name: IntegrationTests
RunOrder: 2
ActionTypeId: { Category: Test, Owner: AWS, Provider: CodeBuild, Version: "1" }
InputArtifacts: [{ Name: SourceOut }]
Configuration: { ProjectName: !Ref IntegTestProject }
- Name: Approval
Actions:
- Name: Manager
ActionTypeId: { Category: Approval, Owner: AWS, Provider: Manual, Version: "1" }
Configuration: { NotificationArn: !Ref ApprovalTopic }
- Name: Production
Actions: [ ... ]
Testing strategy by stage
| Stage | Test type | Typical tool | Goal |
|---|---|---|---|
| Build | Unit, lint, SAST, secret scan | CodeBuild, CodeGuru | Fast feedback, minutes |
| Test / staging | Integration, contract, API tests | CodeBuild in VPC, Step Functions | Verify services together |
| Pre-prod | End-to-end, performance, load, security (DAST) | CodeBuild, Distributed Load Testing | Production-like validation |
| Production | Smoke tests, canaries, synthetic monitoring | CloudWatch Synthetics, CodeDeploy hooks | Detect regressions after release |
Cross-account pattern
A central tooling account hosts the pipeline. Each target account has a deployment role trusted by the pipeline role, and the pipeline's artifact bucket and KMS key policies grant those roles access. CloudFormation actions use a CloudFormation service role in the target account, so the pipeline role itself only needs iam:PassRole and sts:AssumeRole.
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222233334444:role/prod-cfn-deploy-role" },
"Action": ["kms:Decrypt", "kms:DescribeKey", "kms:GenerateDataKey*"],
"Resource": "*"
}
Approvals, Lambda and Step Functions
- Manual approval can include a review URL and comment. Use it before production for regulated environments.
- Lambda invoke action runs custom logic (smoke test); the function must call
PutJobSuccessResultorPutJobFailureResult. - Step Functions action starts a state machine for complex test orchestration.
- Custom actions or the CodePipeline Commands action (V2) run shell commands without a separate CodeBuild project.
Failure handling and observability
- V2 supports stage-level on-failure conditions, including automatic rollback to the last successful execution, and entry conditions (for example only deploy when an alarm is not firing).
- EventBridge pattern for notifications:
{
"source": ["aws.codepipeline"],
"detail-type": ["CodePipeline Pipeline Execution State Change"],
"detail": { "state": ["FAILED"] }
}
Pipeline as code
Define pipelines with CloudFormation or CDK Pipelines, which creates self-mutating pipelines: the first stage updates the pipeline itself when its definition changes.
Limits worth remembering
A pipeline can have up to 50 stages and 50 actions per stage (V2 raises defaults); artifact size per action depends on provider (CodeCommit zips are limited by the artifact store, CloudFormation template size is 1 MB via S3).
Test types, stages and health signals
| Test type | Where in the pipeline | Typical AWS implementation |
|---|---|---|
| Unit tests and coverage | Build stage, every commit and pull request | CodeBuild with test reports |
| Static analysis and security scans | Build or dedicated stage | CodeGuru, Inspector ECR scan, cfn-guard, SAST tools |
| Integration tests | After deploy to a test environment | VPC-enabled CodeBuild calling the deployed stack |
| Acceptance and UI tests | Staging | Selenium or CloudWatch Synthetics canaries |
| Load and stress tests | Pre-production, off-peak | Distributed Load Testing on AWS, containers on Fargate |
| Smoke tests | Right after production deploy | Synthetics canary or CodeDeploy validation hooks |
Pipelines measure application health through exit codes and alarms: a non-zero exit from a test command fails the action, and CodeDeploy or CloudFormation can roll back when a CloudWatch alarm fires. Use pull request triggers (CodeBuild webhook or a PR pipeline) to run unit tests before merge. CodePipeline can invoke other AWS services as actions (Lambda, Step Functions, CloudFormation, CodeBuild), so tests can orchestrate AWS APIs directly.
Real World Example
Scenario: a payments company needs auditable promotions. A tooling account owns one pipeline per service. The pipeline builds and runs unit tests, deploys to a staging account with CloudFormation, runs integration tests via CodeBuild and load tests with a Step Functions workflow, then waits for a manual approval from the release manager. Only then does it deploy to the production account using a cross-account role.
An EventBridge rule posts failures to the team's chat channel, and V2 stage rollback restores the previous release on failed production deployments. Auditors see every approval and artifact version in the execution history.
Architecture Diagram
One tooling account promotes the same artifact through staging and production.
- 1A commit starts the pipeline in the tooling account.
- 2CodeBuild compiles, runs unit tests and scans, producing one artifact.
- 3The pipeline assumes a role in staging, deploys, and runs integration and load tests.
- 4A manual approval records who authorised the release.
- 5The same artifact is deployed to production, followed by smoke tests and alarm monitoring.
Best Practices
- ✓Use a dedicated tooling account and cross-account roles with least privilege.
- ✓Order tests cheapest to slowest, and fail fast.
- ✓Pass a service role to CloudFormation actions rather than giving the pipeline role broad IAM rights.
- ✓Define pipelines as code (CloudFormation / CDK Pipelines) and review pipeline changes like application code.
- ✓Route pipeline failures to EventBridge notifications and add rollback or entry conditions.
Common Interview Questions
Exam Tips
DOP-C02 focus
- "Require sign-off before production" = manual approval action (SNS notification).
- Cross-account deploys fail with KMS errors = CMK key policy missing target roles.
- "Notify on failure" = EventBridge rule or notification rule on pipeline state change.
- Run integration tests in a VPC-enabled CodeBuild project when they need private resources.
Finished this lesson?
0 of 44 lessons done · 0% of the course

