CDK For Terraform
If you want to provision infrastructure using familiar programming languages rather than using the HashiCorp Configuration Language (HCL), you can now do so using the Cloud Development Kit for Terraform (CDKTF). This will let you leverage the power of your existing toolchain for dependency management, testing, and so on.
Presently, the support languages are Typescript, Java, Python, C#, and Go.
Just as you write application code, you can write code that will provision your infrastructure when executed with CDKTF. What happens is, CDKTF translates your application code into Terraform files that will be deployed automatically by Terraform.
Benefits of using CDKTF
Besides writing provision code in the same way that you develop an application, CDKTF lets you use the same tools such as your IDE to write provision code. You can also add more logic to the code provisioning using the programming language’s existing toolset such as conditionals, specific input/output, testing, abstraction, etc.)
How does CDKTF work?
CDK for Terraform leverages libraries and concepts from AWS CDK to translate your code into infrastructure configuration files for Terraform.
You typically will go through the following steps:
- Create an application by scaffolding a project in your chosen language using either a custom template or a built-in template.
- Then, define the infrastructure that you want to provision on one or more providers using your chosen language. CDKTF will extract the schema from Terraform providers and modules automatically so that it can generate the required classes for your application. This can take some time to generate, but is only necessary once.
- Lastly, you will use the cdktf CLI commands to synthesize your code into a JSON terraform file that can be used by Terraform directly to provision your infrastructure. You can also utilize all the Terraform modules and providers available on the Terraform Registry with CDKTF. It’s also available on their commercial product Terraform Cloud and their Enterprise edition.
What is the ideal Usage for CDKTF?
You should investigate whether to use CDKTF when:
- You want developers to provision infrastructure code and let them do that in a programming language they’re familiar with
- You need to manage complexities using abstractions such as when you want to create constructs to model a reusable infrastructure pattern composed of many resources and convenience methods.
- You have a strong preference for a procedural language or need to use one to define infrastructure.
Currently, CDKTF still in beta, so you should be comfortable with doing your own troubleshooting and don’t need commercial support until it’s stable.
You should choose the supported language that you are most familiar with and that fits your company’s present tooling.