Key concepts in CDK– Programmatic Approach to IaC with AWS CDK

The three most important CDK concepts are constructs, stacks, and apps. All of them are represented as classes in the respective programming languages. Let’s clearly define the role of each of these concepts in realizing your infrastructure on AWS.

Constructs

A CDK construct is the underlying AWS resource that you would like to manage as part of your infrastructure definitions. So, anything like an S3 bucket, Lambda function, or a DynamoDB table would be defined as a construct in your CDK code. As you might imagine, this is already available for reuse as AWS does not want you to invest time in developing low-level components that are commonly used across different use cases. Constructs are further divided into three categories:

  • L1 constructs (raw): These are the raw AWS resources without any bells or whistles. Every AWS CloudFormation resource has an equivalent L1 construct that you could use. They are automatically generated from the CloudFormation specification.
  • L2 constructs (curated): These are the AWS resources with some intent baked in. As an example, if you are creating an S3 bucket, then some sane defaults such as disabling public access and enabling data encryption at rest are some of the aspects that are already taken care of.
  • L3 constructs (custom abstractions): These represent a certain pattern that can be reused across a variety of business use cases. It is a collection of L1 and L2 constructs tied together to represent a specific real-world requirement. They only expose the most essential configuration parameters you would like the users to configure. The rest is designed for easy consumption; for this reason, L3 constructs can sometimes be opinionated about how they use certain resources in that pattern.

In addition to building end user-focused applications with CDK, you can use it to create reusable libraries that can then be used by other teams in your organization. This boosts agility and standardization in terms of implementing best practices.

Stacks

Stacks are a collection of one or more constructs that you would like to manage as part of an independent CloudFormation template. We will cover the development workflow when working with CDK shortly, where we will further clarify how these CloudFormation templates are generated under the hood.

Apps

An AWS CDK app refers to the entire application that you code in one of the supported programming languages. An app is composed of several stacks. This corresponds to a certain business use case the application is expected to fulfill and the scope includes all the underlying components, or AWS resources, you would like to manage as part of this application.

When using CDK for authoring your infrastructure templates, a defined workflow needs to be followed.

Let’s discuss what happens at which stage and how your AWS resources are deployed in an account.

Leave a Comment