AWS CDK Pipeline

learn AWS CDK Pipelines

Example CI/CD pipeline and solution code exist in single CDK project / repo in all authored in same language (typescript). Any commits to main automatically trigger a CodePipeline to run and deploy changes to production. The changes can be to the solution or the pipeline itself.

Summary

  • Simple API Gateway -> Lambda solution to exercise CDK pipeline capabilities. via AWS Solutions Constructs | aws-apigateway-lambda
  • Creation of PreProd enviroment (stack) with e2e integration tests.
  • Creation of Prod environment (stack)
  • Automated creation of metrics, alarms, and notification (sns email) for API Gateway and Lambda resources via awslabs/cdk-watchful
  • Automated notification (sns email) of CodePipeline state change events (STARTED, SUCCEEDED, FAILED) via EventBridge -> SNS rule.

High-level Development Workflow

  1. Create solution stack lib/aws-cdk-pipeline-playground-stack.ts (API gateway -> Lambda).
  2. Create stage lib/aws-cdk-pipeline-demo-stage.ts(CdkpipelinesDemoStage) that wraps the solution stack (AwsCdkPipelinePlaygroundStack) for CodePipeline
  3. Create pipeline stack lib/aws-cdk-demo-pipeline-stack.ts and add CdkpipelinesDemoStage stage to it

Steps

npx cdk init --language=typescript

CDK_VERSION="1.97.0"

# install pipeline deps
npm install \
    @aws-cdk/aws-codepipeline@$CDK_VERSION \
    @aws-cdk/aws-codepipeline-actions@$CDK_VERSION \
    @aws-cdk/pipelines@$CDK_VERSION

# leverages `@aws-solutions-constructs/aws-apigateway-lambda`
# see https://docs.aws.amazon.com/solutions/latest/constructs/aws-apigateway-lambda.html
# note CDK version must match `aws-solutions-constructs` version (e.g. $CDK_VERSION)
npm i \
    @aws-cdk/aws-apigateway@$CDK_VERSION \
    @aws-cdk/aws-lambda@$CDK_VERSION \
    @aws-solutions-constructs/aws-apigateway-lambda@$CDK_VERSION

# build locally
npm run build

# test
npm run test

# create *this* repo in github and do initial push to ensure it exists for
# CodePipeline to find

# provision pipeline

# [optional] if not already ran.  bootstrap for each target account+region combination
cdk bootstrap aws://AWS_ACCOUNT_NUMBER/us-east-1
cdk bootstrap aws://AWS_ACCOUNT_NUMBER/us-west-1

# ensure `cdk-pipeline-01-github-token` exists in Secrets Manager

# one-time operation, deploy the pipeline stack from local machine
cdk deploy  --force --require-approval never

# add stage to pipeline.  this is the api gateway -> lambda stack
code lib/aws-cdk-demo-pipeline-stack.ts # edit

# push changes and pipeline will run and deploy PreProd stage
npm run build
git commit -am 'Add PreProd stage'
git push

# the pipeline automatically reconfigures itself to add the new stage and
# deploy to it

# modify solution and/or pipeline, commit, and iterate

Notes

Each pipeline stage is compiled into it’s own cloud assembly as follows:

  • cdk.out/assembly-Dev
  • cdk.out/assembly-CdkpipelinesDemoPipelineStack-PreProd
  • cdk.out/assembly-CdkpipelinesDemoPipelineStack-Prod

You can deploy and individual stage (cfn stack) by itself. For example, for dev.

npm run build # If necessary, to recompile the Lambda sources
cdk synth
cdk -a cdk.out/assembly-Dev deploy --force --require-approval never

# ensure dev account is bootstrapped first
npx cdk bootstrap \
  --cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess \
  aws://DEVELOPER_ACCOUNT/us-east-1

Screenshots

AWS Console | CodePipeline

SNS Email Notifications

CodePipeline State Change COMPLETED

API Gateway 5XX Count Threshold Hit

Lambda Error Count Threshold Hit

Resources


original cdk generated README.md below


Welcome to your CDK TypeScript project!

This is a blank project for TypeScript development with CDK.

The cdk.json file tells the CDK Toolkit how to execute your app.

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template