AWS CDK to Terraform Converter

Turn an AWS CDK stack into Terraform HCL, with the resources and IAM permissions that CDK constructs create behind the scenes written out explicitly.

Your codeAWS CDK (TypeScript)
Drop the file to load it
Tab indents · Esc, Tab exits
ResultTerraform (HCL)
Your Terraform (HCL) will appear hereClick Convert to Terraform or press Ctrl Enter
main.tf
Don't paste secrets

How to convert AWS CDK to Terraform

  1. Add your codePaste it into the AWS CDK (TypeScript) editor, drag in a file, or keep the sample to see how it works.
  2. Convert to TerraformClick the button or press Ctrl + Enter. The result streams into the right-hand editor.
  3. Review and use itCopy or download the result, then test it before you rely on it. AI output is a strong first draft, not a guarantee.

Why CDK code gets longer in Terraform

A few lines of CDK can stand for many resources. L2 constructs fill in defaults and create supporting resources: new lambda.Function() creates an IAM execution role, and bucket.grantRead(processor) adds an IAM policy with the S3 read actions. Terraform has no such layer, so the converter writes each of those out as its own resource, such as aws_iam_role, aws_iam_role_policy and aws_s3_bucket_versioning.

Assets need special attention. lambda.Code.fromAsset("lambda") tells CDK to zip and upload a folder at deploy time. In Terraform you point aws_lambda_function at a zip file yourself, often built with the archive_file data source.

The CloudFormation stack still owns the deployed resources after you convert the code. To hand them to Terraform without downtime, set a retain policy on them, remove them from the stack, then adopt them with Terraform import blocks.

Don't delete the CDK stack until its resources have RemovalPolicy.RETAIN deployed. Otherwise CloudFormation deletes the bucket, table or queue along with the stack.

Tips for better results

  • Compare against cdk synth. The synthesized template lists every resource CDK really creates. Check each one has a Terraform equivalent in the output.
  • Paste the whole stack class. Include imports and the App setup so the converter sees the region, environment and every construct.
  • Review IAM carefully. grant* methods expand to specific action lists. Make sure the Terraform policies aren't broader than what CDK granted.
  • Plan before you apply. After importing existing resources, terraform plan should show no changes. Anything else means the converted arguments differ from what's deployed.
  • Split big apps. Convert one stack at a time and give each its own Terraform root module or state file.

Frequently asked questions

Will Terraform take over my existing CDK resources?

Not automatically. The CloudFormation stack still owns them. Set RemovalPolicy.RETAIN on the resources and deploy, remove them from the stack, then bring them into Terraform with import blocks and confirm terraform plan shows no changes.

Why is the Terraform output much longer than my CDK code?

CDK constructs hide resources and defaults, such as execution roles, IAM policies and bucket settings. Terraform has to declare each of them, so the same infrastructure takes more lines.

What happens to Lambda asset code?

CDK zips and uploads asset folders for you. In Terraform you need your own packaging step, for example an archive_file data source that zips the folder, referenced by filename and source_code_hash on the function.

Does it work with CDK v1 code?

Paste it and see, but v1 reached end-of-support on June 1, 2023, and its @aws-cdk/* packages differ from v2's aws-cdk-lib. Results are most reliable with current v2 code.