What is AWS CDK


Borislav Hadzhiev

Last updated: Apr 13, 2022


Photo from Unsplash

What is AWS CDK? #

AWS CDK (Cloud Development Kit) is an AWS service used to provision infrastructure by writing code in Python, TypeScript, JavaScript, .NET, Java.

The code we write gets compiled down to AWS CloudFormation, however CDK provides a higher level of abstraction than CloudFormation and allows us to leverage a whole bunch of officially maintained packages published by the AWS Team that solve common tasks for provisioning resources in the AWS ecosystem.

By using predefined cloud components we are able to provision and manage our AWS infrastructure in a compact, declarative, predictable and repeatable way.

By writing our infrastructure using a programming language like TypeScript or Python instead of a configuration language (yaml or json), we are able to use all of the autocompletion, linting and type checking a modern IDE provides.

I often don't have to go to the cdk docs, because the type definitions my IDE tells me the exact configuration options a resource takes.

What makes up a CDK application? #

At the entry point of a CDK application we have the App construct. You can think of constructs as cloud components that encapsulate the logic for provisioning one or more AWS resources.

Inside of the App construct we define one or more Stacks. A Stack represents a CloudFormation stack.

Inside of the Stack we have constructs (cloud components), written by other developers or ones we have written ourselves.

The composition of all of these cloud components makes up our CDK application.

Let's look at an example of a complete CDK application, written in TypeScript, which provisions an S3 Bucket:

import * as cdk from 'aws-cdk-lib'; import * as s3 from 'aws-cdk-lib/aws-s3'; // define the CDK Stack export class MyStack extends cdk.Stack { constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { super(scope, id, props); // 👇 using the Bucket cloud component const myBucket = new s3.Bucket(this, 'my-bucket'); } } // 👇 App instantiation const app = new cdk.App(); // 👇 Stack instantiation new MyStack(app, `cdk-stack-dev`, { stackName: `cdk-stack-dev`, env: {region: process.env.CDK_DEFAULT_REGION}, });

Just like with CloudFormation, CDK comes with a command line interface for deploying and managing cdk applications - AWS CDK Toolkit.

Reasons to use CDK #

  1. A very intuitive, compact, direct approach to define infrastructure. We are able to use abstractions written by the AWS Team - AWS Constructs Library.

    There is so much logic we don't have to write, because of the sane defaults and the utility methods for service to service interaction provided in the AWS Constructs Library that it ends up being a huge time saver.

  2. We are able to write our application logic and infrastructure using the same programming language and avoid context switching

  3. We are able to test and validate our code, i.e. check for existence of specific configuration properties on a resource or throw an error and exit early when consumers of our cloud components use them incorrectly

  4. Ability to write our own abstractions for cloud components and reuse them all throughout our code base.

  5. Ability to use programming logic like conditionals and loops

Summary #

AWS CDK is the next logical level of abstraction after CloudFormation. It does the same job, only does it with a focus on developer experience.

Humans are better at reading and writing code using programming languages like TypeScript or Python, whereas computers do just fine with configuration languages like YAML or JSON, which is what CloudFormation is written in.

All of the benefits that come with using CDK to provision our infrastructure are seeded in the ability to use a plain programming language. This opens the door for developer collaboration, pattern definition and code reuse.

For a comparison between CDK and CloudFormation, check out my other article - AWS CDK vs CloudFormation - Comparison

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.