HashiCorp Terraform allows you to define your infrastructure projects using HashiCorp Configuration Language (HCL). HCL is designed to make it easy for people to read and write configuration that is simple and predictable. Some developers prefer to use a familiar programming language to define their infrastructure, however. This allows them to use the same language constructs and tools they are familiar with for other development tasks. They can also use all of the features and libraries available to fully featured programming languages to develop complex infrastructure projects.
The community preview of the Cloud Development Kit for Terraform allows you to define infrastructure using a familiar programming language such as TypeScript, Python, or Go, while leveraging the hundreds of providers and thousands of module definitions provided by Terraform and the Terraform ecosystem.
By using your programming language of choice, you can take advantage of the features and development workflows you are familiar with. For example, when working with Typescript, you can use IDE features such as IntelliSense in Visual Studio Code.
We collaborated with AWS and the Cloud Development Kit (CDK) team to support this new project leveraging two key components of the AWS CDK: a set of language native frameworks for defining infrastructure and adaptors to an underlying provisioning tool. The CDK for Terraform generates Terraform configuration to enable provisioning infrastructure with Terraform. The adaptor works with any existing provider and with the modules hosted in the Terraform Registry. The core Terraform workflow remains the same, with the ability to plan changes before applying.
This is all part of how we've built Terraform: as a platform for provisioning modern infrastructure (or any API supported by Terraform).
»How it works
Terraform can provision infrastructure defined as HCL (HashiCorp Configuration Language) or JSON. HCL is a concise, domain specific language with features JSON lacks such as comments and variables.
The JSON ingest feature of Terraform has been used in CI/CD environments and other machine-generated workflows to integrate with a wide variety of tools. Together with the AWS CDK, this makes it possible to generate JSON from any supported programming language, and then use that JSON to execute standard Terraform commands.
At a high level, the
cdktf tool includes a
deploy command that generates Terraform-friendly JSON configuration from code written in any supported programming language, and then applies that configuration via Terraform.
$ cdktf deploy
Alternatively, you can use cdktf to generate Terraform configuration, and then deploy it with Terraform. The
synth command generates JSON from code written in TypeScript, Python, or other programming languages.
$ cdktf synth
This generates a directory named
cdktf.out with JSON configuration in subdirectories for each of your stacks that can be consumed by Terraform.
$ tree cdktf.out cdktf.out ├── manifest.json └── stacks └── learn-cdktf ├── cdk.tf.json └── plan
Terraform commands can be run in these directories using the
$ cd cdktf.out/stacks/learn-cdktf && terraform apply
We've created several tutorials that you can run to learn how to use this new CDK. In this learning track, you will use the high level
cdktf commands to use TypeScript, Python, and Go code to provision infrastructure on AWS.