Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Cloud Development Kit Can Now Generate Terraform Configurations Using TypeScript and Python

Cloud Development Kit Can Now Generate Terraform Configurations Using TypeScript and Python

This item in japanese

AWS, HashiCorp, and Terrastack collaborated to release a preview of the Cloud Development Kit (CDK) for Terraform, or cdktf. Developers can use programming languages like Python or Typescript to manage infrastructure as code. cdktf generates a Terraform configuration in JSON that can deploy resources with a "terraform apply" command. Also, cdktf supports any existing modules and providers from the Terraform registry to deploy resources to AWS, Azure, or Google Cloud.

CDK is an open-source framework for defining and provisioning infrastructure using programming languages like TypeScript, Java, or Python, similar to other tools like Pulumi or Terrastack. This initial version of cdktf supports only TypeScript and Python, but other languages like Java, JavaScript, or C# might be added later. AWS CDK generates a CloudFormation configuration to deploy the infrastructure. However, CDK for Terraform, or cdktf, creates a Terraform configuration in JSON–not the traditional templates in the HashiCorp Configuration Language (HCL). Therefore, commands like "terraform plan" or "terraform apply" continue to work using the JSON configuration output from the cdktf.

Moreover, developers can use any existing provider and module available in the Terraform Registry. For instance, cdktf can deploy infrastructure resources to other cloud providers like Azure or Google Cloud besides AWS. cdktf can also work with Docker containers or HashiCorp Vault.

Because Terrastack had many similarities with cdktf, its maintainer Sebastian Korfmann is now a maintainer of cdktf, and Terrastack has been archived. The AWS CDK team also contributed to the project by recommending practices and patterns for the CDK construct programming model and the jsii libraries, which is the core of the AWS CDK tool. Chris Fife from AWS and Anubhav Mishra from HashiCorp explained these components' purpose in the release post:

The CDK construct programming model is a set of language native frameworks for defining infrastructure resources and adaptors to generate configuration files for an underlying provisioning engine. The jsii allows code in any supported language to naturally interact with JavaScript classes, enabling the delivery of libraries in multiple programming languages, all from a single codebase. Using these components, the AWS CDK generates CloudFormation configuration from code written in TypeScript, JavaScript, Python, Java, or C#.

In the release announcement, the AWS team included a code example of how to provision an EC2 instance in AWS using TypeScript with the cdktf framework:

import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import { AwsProvider } from './.gen/providers/aws';
import { Instance } from './.gen/providers/aws/instance';
class HelloTerraform extends TerraformStack {
 constructor(scope: Construct, name: string) {
   super(scope, name);
   new AwsProvider(this, 'aws', {
     region: 'us-east-1'
   new Instance(this, 'Hello', {
     ami: "ami-2757f631",
     instanceType: "t2.micro"
const app = new App();
new HelloTerraform(app, 'hello-terraform');

To get started using cdktf, developers need to install the cdktf CLI to initialize a project and generate the Terraform configuration file. Additionally, developers need to download the framework library depending on the programming language of choice. Then, developers can run the "cdktf init --template=typescript" command, which does things like bootstraps the application, downloads dependencies, and asks where to store the project state. The following is an example of the directory and files structure of a cdktf project:

$ tree
├── .gen
│   └── providers
│       └── aws
│        ├── accessanalyzer-analyzer.ts
│        ├── # omitted for clarity
│        └── xray-sampling-rule.ts
│   └── modules
├── .terraform
├── cdktf.json
├── help
├── main.d.ts
├── main.js
├── main.ts
├── package.json
└── tsconfig.json

After a developer finishes defining the infrastructure, cdktf needs to generate the Terraform configuration with the "cdktf synth" command. Then, developers can use the traditional Terraform workflow to download dependencies, validate changes with a plan, and then deploy the infrastructure to the cloud. Or, the same workflow is available using the "cdktf deploy" command. Additionally, cdktf has support to use other providers like Chef in the infrastructure code.

Source: "CDK for Terraform: Enabling Python & TypeScript Support" release post

HashiCorp also released step-by-step guides to get started using cdktf to provision an NGINX server using Docker, deploy an AWS EC2 instance using TypeScript, and other examples for providers like Azure or Google Cloud. The cdktf tool is an experimental project, and the community can contribute by giving feedback or creating a pull request on GitHub.

Rate this Article