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.
- 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 (
FAILED) via EventBridge -> SNS rule.
High-level Development Workflow
- Create solution stack
API gateway -> Lambda).
- Create stage
CdkpipelinesDemoStage) that wraps the solution stack (
AwsCdkPipelinePlaygroundStack) for CodePipeline
- Create pipeline stack
CdkpipelinesDemoStagestage to it
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
Each pipeline stage is compiled into it’s own cloud assembly as follows:
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
cdk-deploy-to.shfor handy deployment script for different account+region combinations. Remember to bootstrap target account+region with
cdk bootstrap aws://account/region
- had to downgrade npm 7 to 6 to get around types error when running
npm run buildon codebuild. see self mutating cdk pipeline fails after updating cdk version from 1.85.0 to 1.92.0 · Issue #13541 · aws/aws-cdk
# steps npm install -g npm@6 rm package-lock.json npm i
AWS Console | CodePipeline
SNS Email Notifications
CodePipeline State Change COMPLETED
API Gateway 5XX Count Threshold Hit
Lambda Error Count Threshold Hit
- CDK Pipelines: Continuous delivery for AWS CDK applications | Amazon Web Services
- cdkworkshop.com | CDK PIPELINES
- @aws-cdk/pipelines module · AWS CDK
original cdk generated
Welcome to your CDK TypeScript project!
This is a blank project for TypeScript development with CDK.
cdk.json file tells the CDK Toolkit how to execute your app.
npm run buildcompile typescript to js
npm run watchwatch for changes and compile
npm run testperform the jest unit tests
cdk deploydeploy this stack to your default AWS account/region
cdk diffcompare deployed stack with current state
cdk synthemits the synthesized CloudFormation template