Terraform Advanced — Terraform CDK

less than 1 minute read

Write Terraform configs in TypeScript, Python, Go, or Java instead of HCL.


What is CDKTF?

Your code (TypeScript) → CDKTF synthesizes → Terraform JSON → terraform apply

You get the full power of a programming language: loops, conditionals, functions, testing, IDE support.


Quick Example (TypeScript)

import { App, TerraformStack } from "cdktf";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { Instance } from "@cdktf/provider-aws/lib/instance";

class MyStack extends TerraformStack {
  constructor(scope: App, id: string) {
    super(scope, id);

    new AwsProvider(this, "aws", { region: "us-east-1" });

    new Instance(this, "app", {
      ami: "ami-0abcdef1234567890",
      instanceType: "t3.micro",
      tags: { Name: "cdktf-demo" },
    });
  }
}

const app = new App();
new MyStack(app, "my-stack");
app.synth();

When to Use CDKTF

✅ Team is stronger in TypeScript/Python than HCL
✅ Complex logic that's awkward in HCL
✅ Want to share patterns as npm/pip packages
✅ Need IDE auto-complete for all resource attributes

❌ Simple infrastructure — HCL is fine
❌ Team already knows HCL well
❌ You want maximum community examples (most are HCL)

Getting Started

npm install -g cdktf-cli
cdktf init --template=typescript
cdktf synth   # Generate Terraform JSON
cdktf deploy  # Apply

What’s Next?

Compare Terraform with Pulumi.

← Terraform Professional Cert Pulumi Comparison →

Updated: