DEV Community

Alex Spinov
Alex Spinov

Posted on

Terraform CDK Has a Free Way to Write Infrastructure as Code in TypeScript or Python

Terraform CDK (CDKTF) lets you define infrastructure using familiar programming languages like TypeScript, Python, Go, or Java — instead of HCL.

What You Get for Free

  • Real programming languages — TypeScript, Python, Java, Go, C#
  • IDE support — autocomplete, type checking, refactoring
  • All Terraform providers — AWS, GCP, Azure, 3000+ providers
  • Terraform state — uses standard Terraform state management
  • Testing — unit test your infrastructure with Jest/pytest
  • Constructs — reusable infrastructure components

Quick Start

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

Define Infrastructure (TypeScript)

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

class MyStack extends TerraformStack {
  constructor(scope: any, id: string) {
    super(scope, id);
    new AwsProvider(this, 'aws', { region: 'us-east-1' });
    new Instance(this, 'server', {
      ami: 'ami-0c55b159cbfafe1f0',
      instanceType: 't2.micro',
    });
  }
}

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

CDKTF vs HCL

Feature CDKTF HCL
Language TS/Python/Go/Java HCL
IDE Full support Limited
Testing Native Terratest
Loops Real code count/for_each

Need infrastructure automation? Check my work on GitHub or email spinov001@gmail.com for consulting.

Top comments (0)