DEV Community

Alex Spinov
Alex Spinov

Posted on

Terraform CDK Has a Free Tool That Lets You Write Infrastructure in TypeScript — No More HCL

The IaC Problem

Terraform's HCL is fine for simple infra. But when you need loops, conditionals, and shared modules? HCL fights you. You end up with for_each hacks and dynamic blocks.

Terraform CDK (CDKTF) lets you write Terraform in TypeScript, Python, Go, or Java. Real programming languages with real abstractions.

What CDKTF Gives You

TypeScript Infrastructure

import { App, TerraformStack } from 'cdktf';
import { AwsProvider } from '@cdktf/provider-aws/lib/provider';
import { Instance } from '@cdktf/provider-aws/lib/instance';
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-app-assets',
      tags: { Environment: 'production' },
    });

    new Instance(this, 'server', {
      ami: 'ami-0c55b159cbfafe1f0',
      instanceType: 't3.micro',
      tags: { Name: 'web-server' },
    });
  }
}

const app = new App();
new MyStack(app, 'production');
app.synth();
Enter fullscreen mode Exit fullscreen mode

Real Loops (Not HCL Hacks)

const environments = ['dev', 'staging', 'production'];

for (const env of environments) {
  new S3Bucket(this, `bucket-${env}`, {
    bucket: `my-app-${env}-assets`,
    tags: { Environment: env },
  });
}
Enter fullscreen mode Exit fullscreen mode

In HCL, this requires for_each with toset(). In TypeScript, it's a for loop.

Reusable Constructs

class WebApp extends Construct {
  constructor(scope: Construct, id: string, props: WebAppProps) {
    super(scope, id);

    const bucket = new S3Bucket(this, 'assets', { bucket: props.domain });
    const cdn = new CloudfrontDistribution(this, 'cdn', { /* ... */ });
    const dns = new Route53Record(this, 'dns', { /* ... */ });
  }
}

// Use it
new WebApp(this, 'marketing-site', { domain: 'example.com' });
new WebApp(this, 'docs-site', { domain: 'docs.example.com' });
Enter fullscreen mode Exit fullscreen mode

Infra components as classes. Share via npm packages.

Uses Terraform Providers

Every Terraform provider works with CDKTF. AWS, GCP, Azure, Cloudflare, Datadog, PagerDuty — 3,000+ providers.

Deploy

cdktf synth   # Generate Terraform JSON
cdktf deploy  # Apply changes
cdktf diff    # Preview changes
Enter fullscreen mode Exit fullscreen mode

Quick Start

npm install -g cdktf-cli
cdktf init --template typescript
cdktf deploy
Enter fullscreen mode Exit fullscreen mode

Why This Matters

HCL works for small infra. For complex, multi-environment setups, TypeScript gives you the abstractions that HCL can't — functions, classes, generics, npm packages.


Automating data infrastructure? Check out my web scraping actors on Apify Store — data collection at scale. For custom solutions, email spinov001@gmail.com.

Top comments (0)