CDKTF (Cloud Development Kit for Terraform) lets you define infrastructure using TypeScript, Python, Go, or Java instead of HCL. Most DevOps engineers don't know about the powerful programmatic APIs it exposes.
What is CDKTF?
CDKTF generates Terraform JSON from real programming languages. You get loops, conditionals, type checking, and IDE autocomplete for your infrastructure.
The Free APIs You're Missing
1. Constructs — Composable Infrastructure Components
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { Instance } from "@cdktf/provider-aws/lib/instance";
class WebServer extends Construct {
public readonly publicIp: string;
constructor(scope: Construct, id: string, props: { ami: string; size: string }) {
super(scope, id);
const instance = new Instance(this, "instance", {
ami: props.ami,
instanceType: props.size,
tags: { Name: id },
});
this.publicIp = instance.publicIp;
}
}
Reusable infrastructure components with TypeScript interfaces. Share across teams via npm.
2. Aspects — Cross-Cutting Infrastructure Policies
import { IAspect } from "cdktf";
import { IConstruct } from "constructs";
class TaggingAspect implements IAspect {
visit(node: IConstruct) {
if (node instanceof Instance) {
node.addOverride("tags.ManagedBy", "cdktf");
node.addOverride("tags.Environment", process.env.ENV || "dev");
}
}
}
// Apply to entire stack
Aspects.of(stack).add(new TaggingAspect());
Automatic tagging, encryption enforcement, naming conventions — applied to every resource.
3. Remote State — Type-Safe Cross-Stack References
import { DataTerraformRemoteState } from "cdktf";
class AppStack extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const networkState = new DataTerraformRemoteState(this, "network", {
organization: "my-org",
workspaces: { name: "network-prod" },
});
const vpcId = networkState.getString("vpc_id");
// Use vpcId with full type safety
}
}
4. Testing — Unit Test Your Infrastructure
import { Testing } from "cdktf";
import { Instance } from "@cdktf/provider-aws/lib/instance";
describe("WebServer", () => {
it("creates an instance with correct tags", () => {
const app = Testing.app();
const stack = new MyStack(app, "test");
const synth = Testing.synth(stack);
expect(synth).toHaveResource(Instance);
expect(synth).toHaveResourceWithProperties(Instance, {
instance_type: "t3.micro",
});
});
});
Unit test infrastructure before deploying. Catch misconfigs in CI, not production.
5. Iterators — Dynamic Resource Generation
import { TerraformIterator } from "cdktf";
const services = ["api", "web", "worker"];
const iterator = TerraformIterator.fromList(services);
new SecurityGroup(this, "sg", {
forEach: iterator,
name: `${iterator.value}-sg`,
ingress: [{ fromPort: 443, toPort: 443, protocol: "tcp" }],
});
Dynamic for_each with real programming language power.
Getting Started
npm install -g cdktf-cli
cdktf init --template=typescript
cdktf deploy
Need data from any website delivered as clean JSON? I build production web scrapers that handle anti-bot, proxies, and rate limits. 77 scrapers running in production. Email me: Spinov001@gmail.com
Check out my awesome-web-scraping list for the best scraping tools and resources.
Top comments (0)