Cloud Development Kit (CDK)
Icon
About
Define your cloud infrastructure using familiar language.
Supported languages includes
Java
,Python
,Typescript/Typescript
,.NET
.
Contains high level components called
constructs
.The code is then compiled into a CloudFormation Template(CFT) (json/yaml). So this is step above
CFT
.You can therefore deploy infrastructure and application runtime code together.
It is great for Lambda functions
Great for docker container
ECS
/EKS
CDK and SAM
CDK and SAM can be used to test your
CDK
apps locally.For this first run
cdk synth
command. This will generate aCFT
.Secondly run
sam local invoke -t <template file> <function-name>
to call the function.
CDK construct
CDK construct
is a component that encapsulates everything CDK needs to create the final CloudFormation stack.Can represent a single or group of AWS resources.
Construct Hub
is a community that contains AWS, third-party and open source CDKs.Constructs are generated based on CloudFormation Resource Specification.
The name of construct starts with
Cfn
.
Levels of CDK
Level 1
CFN Resources
Represents all resources directly available in Cloud Formation
Level 2
Intent Based
Similar to Level 1, but with sensible defaults and boilerplates, provides method
Level 3
Pattern based
Represent multiple related resources, helps to complete most common tasks
CDK Testing
To test CDK apps, use CDK Assertions module, combined with popular test frameworks like
Jest
orPytest
.Two types of tests
Fine-grained Assertions
Snapshot Tests
Working with CDK CLI
Install the CDK, requires
nodejs
.sudo npm install -g aws-sdk-lib
Initialize the
CDK
applicationcdk init app --language <language> ## Example cdk init app --language javascript
Returns CDK app stack
cdk ls
Add your code to the
CDKAppStack
created in step 3 to the filecdk-app-stack.js
.Make the required setup based on your infrastructure added in step 4.
Run the command
cdk bootstrap
once per account per region. This will usecdk.json
file present in the root of directory to create the necessaryCDKToolKit
and necessary resources like AWS S3 and IAM roles before it can deploy into an AWS environment.AWS Environment = account and region
Run the command
cdk synth
optionally to generate theCFT
.Run the command
cdk deploy
to deploy the application.Run the command
cdk destroy
to destroy the stack created in deployment.cdk diff
will give you difference between Local CDK and deployed stack.
Last updated