Why Terraform CDK Changes Everything
If you have ever struggled with HCL syntax in Terraform, CDK for Terraform (CDKTF) lets you define infrastructure using TypeScript, Python, Java, C#, or Go — languages you already know.
The game-changer: You get full IDE support, type checking, and the entire npm/pip ecosystem while still using Terraform providers.
Quick Start
npm install -g cdktf-cli
cdktf init --template=typescript
Define Infrastructure in TypeScript
import { App, TerraformStack } from "cdktf";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { S3Bucket } from "@cdktf/provider-aws/lib/s3-bucket";
class MyStack extends TerraformStack {
constructor(scope: App, id: string) {
super(scope, id);
new AwsProvider(this, "aws", { region: "us-east-1" });
new S3Bucket(this, "bucket", {
bucket: "my-cdktf-bucket",
tags: { Environment: "dev" },
});
}
}
const app = new App();
new MyStack(app, "my-stack");
app.synth();
Key Benefits
-
Type safety — catch errors before
terraform apply - Loops and conditions — real programming constructs, not HCL workarounds
- Code reuse — share infrastructure as npm packages
- Testing — unit test your infrastructure with Jest
- All Terraform providers — AWS, GCP, Azure, Kubernetes, and 3,000+ more
Testing Infrastructure
import { Testing } from "cdktf";
import { MyStack } from "./main";
test("should create S3 bucket", () => {
const app = Testing.app();
const stack = new MyStack(app, "test");
const synth = Testing.synth(stack);
expect(synth).toHaveResource("aws_s3_bucket");
});
Convert Existing HCL to CDKTF
cdktf convert --file main.tf --language typescript
This converts your existing Terraform configs to TypeScript automatically.
CDKTF vs Raw Terraform
| Feature | HCL | CDKTF |
|---|---|---|
| Type checking | Limited | Full IDE support |
| Loops |
for_each, count
|
Native for/map/filter |
| Testing | terraform validate |
Jest, pytest |
| Code sharing | Modules | npm/pip packages |
| Learning curve | New syntax | Your existing language |
Resources
Need to scrape infrastructure data, cloud pricing, or provider APIs at scale? I build custom data extraction tools. Check out my web scraping actors on Apify or email me at spinov001@gmail.com for custom solutions.
Top comments (0)