tf2cdk

Convert Terraform to AWS CDK

GitHub stars MIT License Version 0.1.0 Python 3.8+

Terraform → CDK Migration Made Easy

For teams moving entirely off Terraform to CloudFormation

# Homebrew (macOS)
$ brew tap jtaylortech/tf2cdk
$ brew install tf2cdk
# Or via pip
$ pip install tf2cdk
$ tf2cdk convert ./terraform --output ./cdk-app --language typescript
# Converting Terraform to CDK...
✓ Parsed 24 resources
✓ Generated TypeScript CDK code
✓ Saved to ./cdk-app

tf2cdk vs cdktf: Different Tools, Different Problems

tf2cdk (this project)

  • Convert Terraform → native CDK
  • Uses CloudFormation engine
  • Outputs CDK code, CF stacks
  • Runs cdk deploy

cdktf (CDK for Terraform)

  • Write CDK → generates Terraform
  • Uses Terraform execution engine
  • Outputs HCL, uses TF state
  • Runs terraform apply

Install

Homebrew (macOS)

brew tap jtaylortech/tf2cdk
brew install tf2cdk

pip

pip install tf2cdk

Why Migrate to Native CDK?

Control Tower

Requires CloudFormation for account factory

LZA

Landing Zone Accelerator built on CloudFormation

Service Catalog

Native CloudFormation integration

Enterprise Standards

CloudFormation compliance requirements

Example Conversion

CDK TypeScript (Output)

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';

export class InfrastructureStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string) {
    super(scope, id);

    const dataBucket = new s3.Bucket(this, 'DataBucket', {
      bucketName: 'my-data-bucket',
      versioned: true,
      tags: {
        Environment: 'production',
      },
    });
  }
}

Terraform (Input)

resource "aws_s3_bucket" "data" {
  bucket = "my-data-bucket"

  tags = {
    Environment = "production"
  }
}

resource "aws_s3_bucket_versioning" "data" {
  bucket = aws_s3_bucket.data.id

  versioning_configuration {
    status = "Enabled"
  }
}

Features

Quick Start

# Convert to TypeScript CDK
tf2cdk convert ./terraform --output ./cdk-app --language typescript

# Convert to Python CDK
tf2cdk convert ./terraform --output ./cdk-app --language python

# Preview without writing files
tf2cdk convert ./terraform --output ./cdk-app --dry-run

# Generate import commands for existing resources
tf2cdk import-plan ./terraform --statefile terraform.tfstate

Documentation

Full documentation, migration guides, and troubleshooting available on GitHub.

Support tf2cdk

If tf2cdk saves you weeks of migration work, consider sponsoring development.