SkillSprout
Chapter 1 · Lesson 4
Advanced
6 min read

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

yaml
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

StageTest typeTypical toolGoal
BuildUnit, lint, SAST, secret scanCodeBuild, CodeGuruFast feedback, minutes
Test / stagingIntegration, contract, API testsCodeBuild in VPC, Step FunctionsVerify services together
Pre-prodEnd-to-end, performance, load, security (DAST)CodeBuild, Distributed Load TestingProduction-like validation
ProductionSmoke tests, canaries, synthetic monitoringCloudWatch Synthetics, CodeDeploy hooksDetect 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.

json
{
  "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 PutJobSuccessResult or PutJobFailureResult.
  • 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:
json
{
  "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 typeWhere in the pipelineTypical AWS implementation
Unit tests and coverageBuild stage, every commit and pull requestCodeBuild with test reports
Static analysis and security scansBuild or dedicated stageCodeGuru, Inspector ECR scan, cfn-guard, SAST tools
Integration testsAfter deploy to a test environmentVPC-enabled CodeBuild calling the deployed stack
Acceptance and UI testsStagingSelenium or CloudWatch Synthetics canaries
Load and stress testsPre-production, off-peakDistributed Load Testing on AWS, containers on Fargate
Smoke testsRight after production deploySynthetics 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

Cross-account pipeline with quality gates
Architecture
AWS Organization
Tooling account
Source
Build + unit tests
CodePipeline
Staging account
Deploy stack
Integration + load tests
Production account
Deploy stack
Smoke tests + alarms

One tooling account promotes the same artifact through staging and production.

  1. 1A commit starts the pipeline in the tooling account.
  2. 2CodeBuild compiles, runs unit tests and scans, producing one artifact.
  3. 3The pipeline assumes a role in staging, deploys, and runs integration and load tests.
  4. 4A manual approval records who authorised the release.
  5. 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