Virtual Event
Join us for the next HashiConf Digital October 12-15, 2020 Register for Free

Write Terraform with Typescript and Python

beta

Introduction to cdktf

As a developer, you're accustomed to using your own tooling and writing in familiar languages. For several years, HCL has been one of the fastest growing languages at GitHub, and yet we have often heard the desire to provision infrastructure with familiar programming languages like Python and TypeScript.

The community preview of the Cloud Development Kit for Terraform allows you to define infrastructure using TypeScript and Python while leveraging the hundreds of providers and thousands of module definitions provided by Terraform and the Terraform ecosystem.

By leaning on languages like TypeScript, you can use all the IDE features you depend on, such as IntelliSense in Visual Studio Code.

Autocomplete

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 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).

Terraform as Platform

»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 TypeScript or Python, then run that JSON using standard Terraform commands.

At a high level, the cdktf tool includes a deploy command that can compile TypeScript or Python, generate Terraform-friendly JSON, and apply the data definition via Terraform.

$ cdktf deploy

Or, the workflow can be performed manually in smaller steps. The synth command generates JSON from TypeScript or Python code.

$ cdktf synth

This generates a directory named cdktf.out which can be consumed by Terraform.

$ tree cdktf.out

cdktf.out
├── cdk.tf.json
└── plan

Terraform commands can be run in the cdktf.out directory using the cdk.tf.json file.

$ cd cdktf.out && terraform apply

»Next steps

We've created several tutorials that you can run to learn how to use this new CDK. In this learning track, we'll use the high level cdktf commands to work with TypeScript code directly.