The AWS Cloud Development Kit (AWS CDK) allows you to define your AWS infrastructure in a common programming language. The CDK supports the following languages: Typescript, JavaScript, Python, Java, and C#. Though all languages are transpiled to JavaScript in the end.
The CDK was first released on 31 July 2018 (v0.8.0). Since then it was developed with great pace and is currently available in version 1.22.
The broad language support and the fast release cycles show AWS’s motivation to provide good tooling for the developers.
However, the downside is, that new releases often come with a lack of backward compatibility and leave outdated documentation pages in their wake.
AWS considered the migration path for those who were already working with CloudFormation (CFN) templates because they offer first class support for CFN [1].
You can import the template with the aws-cloudformation module [2].
On the other hand, CDK always uses CloudFormation under the hood when deploying to the cloud. Internally, the CDK code generates a CloudFormation template and runs the CloudFormation executor. This will give you the advantage of CloudFormation’s transactional deployments deployments, making rollbacks possible. The CloudFormation based deployment is indeed very stable but also quite slow.
When the CloudFormation template is generated, CDK also fills in a lot of defaults that you don’t have to specify explicitly in your code. In that respect, you can see the CDK as a templating engine for CloudFormation. If you want to have a look at the CloudFormation output, you can simply run cdk synth in the CLI, which synthesises and prints the CloudFormation template for this stack.
A great advantage of CDK is that you can enjoy the expressiveness of your preferred language alongside value-added benefits: the respective library ecosystem, code completion, and type-safety. For example, decisions based on environment variables or iterating over collections (like different Lambdas services) can be easily implemented.
An important concept in CDK is Constructs; a construct is a base for all elements in CDK. There are certain types of constructs:
- App: a special top-level construct / root node:
- Bracket around Stack constructs
- Stack: a special second-level)construct:
- Run cdk list to see all stacks in the app. A stack is a deployable unit in a CDK app. Tun cdk deploy stackname1 stackname2 to deploy the respective stacks by name. Note that nested stacks are not written out by cdk list.
- Constructs like Lambda or API Gateway are provided by AWS and represent your actual cloud resources. Think of your CDK app as a tree and those Constructs are the leaf nodes.
- Constructs can point to other constructs or hold the actual cloud resources (Composite pattern).
Besides the constructs provided by AWS in the Construct Library, you can build up personal libraries of reusable components. In general, your object-oriented programming (OOP) skills are a good match with the CDK.