This document discusses the AWS Cloud Development Kit (CDK) which allows defining AWS infrastructure as code using regular programming languages. It introduces CDK concepts like stacks, constructs, and the CDK workflow. The rest of the document demonstrates CDK through examples like creating a S3 bucket, developing custom constructs, and unit testing CDK apps. Benefits of CDK are reusable infrastructure defined as code that can be version controlled, tested, and shared as libraries.
15. connect
DevAx
CDK workflow
cdk bootstrap - Deploys the CDK toolkit stack into an AWS environment
cdk init - Create a new CDK project
Add the constructs your code needs
e.g. npm install @aws-cdk/aws-lambda @aws-cdk/aws-apigateway
Compile your code if needed
e.g. npm run build
cdk synth - Synthesizes and prints the CloudFormation template for the stack
cdk deploy - Deploys the stack(s) into your AWS account
cdk destroy - Destroy the stack(s)
17. connect
DevAx
3 Levels of Constructs
1. L1 constructs are the resources defined by CloudFormation
• For example, CfnBucket class represents the AWS::S3::Bucket CFN Resource.
2. L2 constructs have higher-level, intent-based API. Same functionality, but
handle much of the details, boilerplate, convenient default
• For example, the Bucket class represents an Amazon S3 bucket with additional
properties and methods, such as bucket.addLifeCycleRule
3. Even higher-level constructs, which called pa#erns. Common tasks in AWS,
often involving multiple kinds of resources.
18. connect
DevAx
Stacks
The unit of deployment in the AWS CDK is called a stack.
const app = new cdk.App();
new MyFirstStack(app, "stack1");
new MySecondStack(app, "stack2");
cdk ls
stack1
stack2
cdk synth stack1
To list all the stacks in the app use “cdk ls” command:
To deploy specific stack provide stack name for “cdk synth” command:
28. connect
DevAx
Unit Testing
• Snapshot tests test the synthesized AWS CloudFormation
template against a previously-stored baseline template
• Fine-grained assertions test specific aspects of the generated AWS
CloudFormation template, such as "this resource has this property
with this value.”
• Validation tests help you "fail fast" by making sure your AWS CDK
constructs raise errors when you pass them invalid data.
29. connect
DevAx
Unit Testing
• Use Jest testing framework
• $ npm install --save-dev jest @types/jest @aws-cdk/assert
• $ npm test
33. connect
DevAx
AWS Solutions Constructs
A Library of Architecture Patterns for the AWS CDK
import { KinesisStreamsToKinesisFirehoseToS3 } from '@aws-solutions-constructs/aws-kinesisstreams-kinesisfirehose-s3’;
new KinesisStreamsToKinesisFirehoseToS3(this, 'test-stream-firehose-s3', {});
link
36. connect
DevAx
Benefits of CDK
• Use logic (if statements, for-loops, etc.) when defining your infrastructure
• Use object-oriented techniques to create a model of your system
• Define high level abstractions, share them
• Reuse your infrastructure as a library
• Use your existing code review workflow
• Provide test framework
• Code completion within your IDE