You can find code example for this chapter here.
The CDK is a library that outputs a CloudFormation template and that template can be deployed to AWS just like any other template. It is available for multiple languages, but we'll focus on JavaScript in this chapter.
CloudFormation is AWS's version of Terraform: you upload a document that describes what resources you want for the app along with their intended configurations and it creates, modifies, and destroys resources in the AWS account to match reality to the intended state. CloudFormation then keeps track of what is deployed so there is no need for a separate state management, which is one of the pain points when working with Terraform.
But writing CloudFormation templates by hand is unintuitive and often very repetitive. The CDK utilizes a real programming language and uses the CloudFormation template as an output representation. That means, while you get all the benefits of CloudFormation you probably never need to look into the templates themselves.
Using the CDK offers several benefits. The more obvious is that it allows you to use a familiar language, such as JavaScript, Typescript, Python, Java, or one of the other supported ones. This gives a lot of power to the otherwise very simple CloudFormation templates, and allows things like easier code reuse and the ability to create similar resources with things like a simple map
function.
Next, it offers a constructs library, which means code can be checked more easily in compile-time.