CDK for Terraform improves performance by adding namespaces

Hashi Corp released Version 0.13 of CDK for Terraform (CDKTF) with significant performance improvements. The release introduces a major overhaul of the language through the introduction of namespaces. The namespace improvement significantly improves the performance of the synthesizing infrastructure. This version builds on version 0.12 into which CDKTF has moved General availability.

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

Version 0.13 introduces the concept of namespaces in each class in the generated provider bindings. These namespaces are automatically derived from the Terraform resource or data source they come from. Previously, each vendor’s exports were a flat list of components. This resulted in large packets that the various language compilers struggled to process. CDKTF users reported that the synthesis could be very slow and could cause the IDE to crash for Python users.

The namespace change creates a bunch of small packages, which is faster for the compilers to process. This has led to sometimes significant improvements in processing time Nara Kasbergen Kwon, Technical Director at Hashicorp. Kwon shares what recent benchmarking tests show:

  • A 96.8% reduction in cdktf synth time when using Go with the Azure provider
  • an 83 percent reduction in cdktf synth time when using Java with Google Cloud Provider
  • A 36.8% reduction in cdktf synth time when using C# with the AWS provider
  • A 61.5% reduction in cdktf synth time when using TypeScript with the Kubernetes provider

The introduction of namespaces is a breakthrough change, but version 0.13 is backwards compatible with provider bindings generated by version 0.12. This allows mixing namespace and non-namespace providers as needed. Note that providers generated by version 0.13 are namespaced. This backwards compatibility is removed in version 0.14 and only namespace providers are supported.

The AWS provider previously had a namespace, but in a way that differed from the new namespace method. Therefore, the way of importing has changed to adapt to the new namespace model. Previously, the AWS provider could be imported into Go as follows:

import (
 // ... other imports
 "github.com/cdktf/cdktf-provider-aws-go/aws"
)

func NewMyStack(/* ... */) cdktf.TerraformStack {
 stack := cdktf.NewTerraformStack(/* ... */)

 aws.NewAwsProvider(/* ... */)
 aws.NewCloudfrontDistribution(/* ... */)
 aws.NewAcmCertificate(/* ... */)
}

With the new namespace model, it now needs to be imported as follows:

import (
 // ... other imports
 "github.com/cdktf/cdktf-provider-aws-go/aws/cloudfrontdistribution"
 "github.com/cdktf/cdktf-provider-aws-go/aws/provider"
 "github.com/cdktf/cdktf-provider-aws-go/aws/acmcertificate"
)

func NewMyStack(/* ... */) cdktf.TerraformStack {
 stack := cdktf.NewTerraformStack(/* ... */)

 provider.NewAwsProvider(/* ... */)
 cloudfrontdistribution.NewCloudfrontDistribution(/* ... */)
 acmcertificate.NewAcmCertificate(/* ... */)
}

According to Kwon, the upcoming version 0.14 is planned for a release in mid-November 2022. Kwon shares that the release will “mainly focus on quality of life improvements that make it easier to use pre-built providers. “The team found that out-of-the-box providers deliver a better developer experience than generating them locally cdktf get.

For more information on the content of the release, see the blog entry and Upgrade Guide. Hashicorp Hosts Community office hours and has one discussion forum for questions. That CDK for Terraform Learning Guide recommended for new users of the CDK.

Comments are closed.