AWS CDK

Also known as: AWS Cloud Development Kit

On this page 7

The AWS CDK (AWS Cloud Development Kit) is an open-source infrastructure as code framework that lets engineers define Amazon Web Services resources in general-purpose programming languages such as TypeScript, Python, Java, C# and Go. Instead of writing long YAML or JSON templates by hand, a team writes ordinary classes and functions, and the CDK synthesizes them into AWS CloudFormation templates that the CloudFormation service then deploys.

In the cloud, the CDK occupies a distinctive position: it combines the expressiveness of a programming language with the managed state and rollback behavior of CloudFormation. It is therefore not a separate provisioning engine but a higher-level authoring layer on top of an existing one, which shapes both its strengths and its limits.

How AWS CDK turns code into deployed infrastructure

The AWS CDK works in two distinct phases: synthesis, which runs locally or in a pipeline, and deployment, which runs inside CloudFormation. During synthesis, the CDK application executes like any other program, builds an in-memory construct tree and writes a cloud assembly, a directory containing CloudFormation templates, asset manifests and metadata.

During deployment, the cdk deploy command uploads file and container assets to a staging bucket and registry, then submits the templates as CloudFormation stacks. CloudFormation computes a change set, creates or updates resources in dependency order and rolls back automatically if a resource fails. The CDK itself keeps no state file; the deployed state lives in CloudFormation.

Before the first deployment, each target account and region needs bootstrapping with cdk bootstrap, which creates the staging bucket, an image repository and IAM roles that the CLI assumes for publishing and deploying. The following table summarizes the main pipeline stages.

StageCommand or artifactWhat happens
Bootstrapcdk bootstrapCreates staging resources and deployment roles once per account and region
Synthesiscdk synthRuns the app and produces the cloud assembly
Reviewcdk diffCompares the synthesized template with the deployed stack
Deploymentcdk deployPublishes assets and executes CloudFormation change sets
Removalcdk destroyDeletes the stack, subject to resource retention policies
Stages of a typical CDK workflow

Constructs, stacks and apps in the CDK model

The central abstraction of the AWS CDK is the construct, a reusable component that represents one or more cloud resources together with their configuration. Constructs nest inside stacks, which map one-to-one to CloudFormation stacks, and stacks belong to an app, the root of the construct tree.

The AWS Construct Library organizes constructs into three levels of abstraction. Choosing the right level determines how much the team writes by hand and how many secure defaults it inherits.

LevelDescriptionTypical use
L1 constructsGenerated one-to-one from CloudFormation resource types, prefixed with CfnFull control or very new service features
L2 constructsCurated classes with sensible defaults and helper methodsMost everyday resources such as buckets and queues
L3 constructs (patterns)Combinations of several resources for a common architectureLoad-balanced container services, API with functions
Construct levels in the AWS Construct Library

L2 constructs add intent-based methods such as grantRead, which generate least-privilege IAM policies automatically. This is one of the strongest practical arguments for the CDK, because hand-written policies are a frequent source of over-permissive access. Teams can also publish their own constructs as packages, which turns organizational standards into versioned, testable libraries distributed through jsii to every supported language.

AWS CDK compared with other infrastructure as code tools

The AWS CDK differs from its neighbors mainly in where state lives, which clouds it targets and how configuration is written. Comparing it with AWS CloudFormation, Terraform and Pulumi clarifies when each one is the better fit.

ToolAuthoring styleState managementCloud scope
AWS CDKTypeScript, Python, Java, C#, GoHeld by CloudFormationAWS only
AWS CloudFormationYAML or JSON templatesHeld by CloudFormationAWS only
TerraformHashiCorp Configuration LanguageTerraform state fileMulti-cloud via providers
PulumiGeneral-purpose languagesPulumi state backendMulti-cloud via providers
Comparison with related tools

The CDK is imperative in authoring but declarative in result: loops, conditions and functions run at synthesis time, yet the output is a static, declarative template. This distinction matters because values that are only known at deployment time, such as generated resource names, appear in code as tokens, placeholders that resolve inside CloudFormation rather than in the program.

When to choose AWS CDK and how to structure projects

The AWS CDK fits best when an organization is committed to AWS, has developers comfortable with a typed language and wants to share infrastructure patterns as code libraries. It is less suitable when infrastructure spans several clouds or when the team prefers a purely declarative configuration language.

SituationRecommended choiceReason
AWS-only platform with application developersAWS CDKSame language and tooling as the application code
Several cloud providersTerraform or PulumiProvider ecosystem covers more than AWS
Small, stable set of resourcesPlain CloudFormationLess tooling and fewer dependencies
Shared platform standards across many teamsCustom CDK construct libraryVersioned defaults enforced through code reuse
Decision guide for adopting the CDK

Proven project practices include separating stateful and stateless stacks, so that databases are not replaced during routine changes, pinning library versions, running cdk diff in every pull request and writing assertion tests against synthesized templates. CDK Pipelines, a construct for continuous delivery, can deploy the same app to several stages and accounts in a controlled sequence.

Limitations and common mistakes with AWS CDK

The AWS CDK inherits every CloudFormation limit, including the cap of 500 resources per stack, slower deployments than direct API calls and occasional lag in supporting new service features at L2. Its abstraction also hides detail: engineers who never read the synthesized template may not notice that a small code change replaces a resource instead of updating it.

ProblemLikely causeFix
Database replaced unexpectedlyChanged logical ID after refactoringKeep construct IDs stable or override logical IDs
Deployment fails on new accountEnvironment not bootstrappedRun cdk bootstrap for that account and region
Non-deterministic templatesNetwork lookups or timestamps during synthesisCommit context values and avoid runtime randomness
Stack too largeHundreds of resources in one stackSplit into nested or separate stacks
Frequent problems, causes and fixes

Configuration drift remains possible when someone changes resources in the console; CloudFormation drift detection can reveal it, but the CDK does not prevent it. A further risk is overengineering: deep class hierarchies make infrastructure harder to review than the plain templates they replace.

In everyday language

The AWS CDK lets programmers describe their Amazon cloud setup as a program, the same way they write their apps. Instead of filling out a long, rigid form that lists every server, database and permission, they write a short program that says what they need. The CDK then turns that program into the detailed form, and Amazon builds everything from it.

Think of it as a kitchen recipe written by a chef compared with a shopping list. The recipe can say “repeat this step for each guest”, and the CDK expands it into the exact list. The key benefit is that good habits, such as safe permissions, are built in and reused instead of being retyped each time.

Seen in the real world

A mid-sized online retailer runs its storefront on AWS with a platform team of four engineers and about twenty application developers. Previously, each team copied CloudFormation templates of 1,500 to 3,000 lines, and security reviews repeatedly found storage buckets without encryption and IAM roles with wildcard permissions.

The platform team built an internal TypeScript construct library with a standard service construct that creates a container service, a load balancer, encrypted logs and alarms with approved defaults. Application teams now define a new service in roughly 40 lines, and every pull request shows a cdk diff output for reviewers. CDK Pipelines deploys each change to test, staging and production accounts in sequence.

Within two quarters, the time to launch a new service dropped from about two weeks to two days, and the recurring security findings largely disappeared because the defaults lived in one versioned library rather than in dozens of copied templates.