--hcl flag when running cdktn synth.
CDKTN may not be the right choice for every team and project within your organization. For example, some teams may already be very familiar with Terraform and have created HCL modules, providers, etc. To provide flexibility, CDKTN applications are interoperable with Terraform projects written in HCL. Specifically:
- CDKTN applications can use all existing Terraform providers and HCL modules.
- CDKTN can generate modules that HCL Terraform projects can use in their configurations.
CDKTN to HCL
The following example is a CDKTN application that uses thehashicorp/random provider to generate a random name.
cdktn synth and copy the resulting cdktf.out/stacks/random-pet-module/cdk.tf.json file out to the module directory in your HCL project.
By default, cdktn synth generates Terraform JSON, but starting from version 0.20, CDKTN can also generate Terraform HCL output by passing the --hcl flag to cdktn synth.
After you transfer the cdk.tf.json (or cdk.tf) file, you can reference the pet name module as you would any other HCL Terraform module.
HCL to CDKTN
HCL can be used with Terraform CDK in two ways. Converting HCL code directly to a CDKTN language, and using Terraform modules directly within CDKTN projects.-
In order to convert HCL to a CDKTN language, the
cdktn convertcommand can be used. It automatically translates HCL into a preferred CDKTN language. This is useful when working with an existing codebase that needs to be converted to CDKTN. -
While CDKTN has the ability to import HCL modules through
cdktn getwhen referenced within thecdktf.jsonfile, Terraform modules can also be referenced without generating language specific bindings. The modules documentation shows how to use existing Terraform modules in CDK Terrain projects.