AWS CDK — Define Cloud Infrastructure Using Real Programming Languages
The AWS Cloud Development Kit lets you define cloud infrastructure in TypeScript, Python, Java, Go, or C# instead of YAML templates. CDK synthesizes your code into CloudFormation and deploys it with a single command.
What it is
The AWS Cloud Development Kit (CDK) lets you define cloud infrastructure using familiar programming languages instead of YAML or JSON templates. Write TypeScript, Python, Java, Go, or C# code that describes your AWS resources, and CDK synthesizes it into CloudFormation templates and deploys them.
CDK provides high-level constructs that bundle multiple AWS resources with best-practice defaults. A single L2 construct can create a Lambda function with its IAM role, log group, and API Gateway integration.
How it saves time or tokens
CloudFormation YAML is verbose and error-prone. A Lambda function with API Gateway can take 100+ lines of YAML. The same setup in CDK takes 10 lines of TypeScript with sensible defaults for IAM, logging, and error handling.
For AI code generation, CDK's type-safe interfaces mean LLMs can produce correct infrastructure code with autocomplete-friendly APIs. TypeScript CDK code benefits from full type checking before deployment.
How to use
- Install CDK and create a project:
npm install -g aws-cdk
cdk init app --language typescript
- Define infrastructure in code:
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class MyApiStack extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
const fn = new lambda.Function(this, 'Handler', {
runtime: lambda.Runtime.PYTHON_3_12,
code: lambda.Code.fromAsset('lambda'),
handler: 'index.handler',
});
new apigateway.LambdaRestApi(this, 'Api', { handler: fn });
}
}
- Deploy:
cdk deploy
- CDK synthesizes CloudFormation, creates the stack, and provisions all resources.
Example
// CDK creates IAM role, log group, and API Gateway automatically
const fn = new lambda.Function(this, 'Processor', {
runtime: lambda.Runtime.NODEJS_20_X,
code: lambda.Code.fromAsset('src'),
handler: 'processor.handler',
timeout: cdk.Duration.seconds(30),
memorySize: 256,
});
// Grant the function read access to an S3 bucket
bucket.grantRead(fn);
Related on TokRepo
- AI Tools for DevOps — Infrastructure-as-code and deployment tools
- AI Tools for Coding — AI-assisted infrastructure development
Common pitfalls
- Not running cdk diff before cdk deploy. Always review the change set to understand what resources will be created, modified, or destroyed.
- Using L1 constructs when L2 exists. L1 constructs are raw CloudFormation mappings. L2 constructs provide higher-level abstractions with best-practice defaults. Always check for L2 first.
- Ignoring CDK context and environment separation. Use cdk.json context and environment-specific stacks to manage dev, staging, and production deployments cleanly.
- Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.
Frequently Asked Questions
CDK uses real programming languages with loops, conditions, and abstractions. Terraform uses HCL, a domain-specific language. CDK is AWS-only (though CDK for Terraform exists). Terraform supports all major cloud providers. CDK generates CloudFormation; Terraform has its own state management.
Yes. CDK supports Python as a first-class language. Install aws-cdk-lib via pip and write stacks in Python. The API is identical in structure to TypeScript, with Python naming conventions.
Constructs are the building blocks of CDK. L1 constructs map 1:1 to CloudFormation resources. L2 constructs provide higher-level abstractions with defaults. L3 constructs (patterns) bundle multiple resources for common architectures like API + Lambda + DynamoDB.
Yes. CDK generates least-privilege IAM policies automatically when you use grant methods (bucket.grantRead, table.grantWriteData). You do not need to write IAM policy JSON manually for most use cases.
CDK can import existing resources into a stack using from* methods (like Bucket.fromBucketName). However, CDK cannot modify resources it did not create unless you explicitly import them into the stack.
Citations (3)
- AWS CDK GitHub— AWS CDK defines cloud infrastructure in programming languages
- AWS CDK Documentation— AWS CDK documentation and construct library
- CloudFormation Documentation— AWS CloudFormation infrastructure provisioning
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.