BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News CDK for Terraform Improves HCL Conversion and Terraform Cloud Interactions

CDK for Terraform Improves HCL Conversion and Terraform Cloud Interactions

Bookmarks

HashiCorp has released a number of new improvements to the CDK for Terraform (CDKTF). These improvements include enhanced type coercion, iterators support, and function support. Other improvements target the experience of working with CDKTF within Terraform Cloud or Terraform Enterprise. This includes improvements to plan and apply and the automated creation of Terraform workspaces.

The CDK allows for writing Terraform configurations in a number of programming languages including C#, Python, TypeScript, Go, and Java. It includes support for all existing Terraform providers and modules. The CDKTF application code synthesizes into JSON output that can be deployed with Terraform directly.

The 0.16 release improves type coercion by updating the convert command to match the type of value being assigned to an attribute. The convert command is used to convert pre-existing Terraform HCL code into a CDKTF-compatible language. The command now compares the type being generated from HCL and matches it to the provider schema. In the event of a type mismatch, the incoming value will be coerced into the correct type. This change prevents errors that could occur in previous releases as Terraform automatically converts primitive types to match the resource schema.

The release also improved the conversion of meta-arguments such as count and for_each. Now the count meta-attribute can be represented as an iterator via an escape hatch. In addition, convert is able to use iterators for for_each, count, and dynamic blocks without an escape hatch.

The following TypeScript example uses the new TerraformCount function to create the specified number of instances as defined within servers.numberValue:

const servers = new TerraformVariable(this, "servers", {
  type: "number",
});

const count = TerraformCount.of(servers.numberValue);

new Instance(this, "server", {
  count: count,
  ami: "ami-a1b2c3d4",
  instanceType: "t2.micro",
  tags: {
    Name: "Server ${" + count.index + "}",
  },
});

HashiCorp recommends using iterators when referencing dynamic data that will not be known until after Terraform applies the configuration. For static data, they recommend using loops within the selected programming language. Escape hatches are needed when referencing a specific index within the list. This is because CDKTF implicitly converts lists to sets when iterating over them.

Conversion can also now convert Terraform functions to the appropriate CDKTF function. This improves the readability of code and permits autocomplete to function properly. With this change, the following HCL expression:

replace("hello-${22+22}", "44", "world")

Would be converted to:

cdktf.Fn.replace("hello-" + cdktf.Token.asString(cdktf.Op.add(22, 22)), "44", "world")

The 0.15 release made a number of improvements to how CDKTF interacts with both the Terraform Cloud and Terraform Enterprise environments. In previous releases, during stack configuration with CDKTF, the interaction with Terraform inside Terraform Cloud was hidden. It could only be viewed through a URL.

This release replaced the Terraform Cloud API-based implementation with one that makes calls directly to the Terraform CLI. This allows for the full plan to be displayed with details. Additionally, CDKTF now supports additional features such as cost estimations and Sentinel policies when run within Terraform Cloud.

The release also introduced automatic workspace creation when running plan and diff or apply and deploy. This improves the previous behavior where workspaces would have to be manually created through the Terraform Cloud or Enterprise UI.

More information about the contents of the release can be found in the blog post and upgrade guide. HashiCorp has a discussion forum for questions. The CDK for Terraform tutorials are recommended for users new to CDKTF.

About the Author

Rate this Article

Adoption
Style

BT