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 Performance by Adding Namespaces

CDK for Terraform Improves Performance by Adding Namespaces

HashiCorp released version 0.13 of the CDK for Terraform (CDKTF) with significant improvements to performance. The release introduces a major restructuring of the language by introducing namespaces. The namespace improvement dramatically enhances the performance of synthesizing infrastructure. This release builds upon the 0.12 release which saw CDKTF move into general availability.

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.

Version 0.13 introduces the concept of namespaces into each class in the generated provider bindings. These namespaces are automatically derived from the Terraform resource or data source it originates from. Previously, each provider's exports were a flat list of components. This resulted in large packages that the various language compilers struggled with processing. Users of CDKTF indicated that synthesizing could be very slow and, for Python users, could cause the IDE to crash.

The namespaces change produces a number of small packages, which is faster for the compilers to process. This has led to some significant improvements in processing time according to Nara Kasbergen Kwon, Engineering Manager at Hashicorp. Kwon shares that recent benchmarking tests show:

  • a 96.8% reduction in cdktf synth time when using Go with the Azure provider
  • an 83% reduction in cdktf synth time when using Java with the 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 breaking change, however, the 0.13 release is backward compatible with provider bindings generated by version 0.12. This allows for the mixing of namespaced and non-namespaced providers as needed. Note that providers generated by version 0.13 will be namespaced. This backward compatibility will be removed in version 0.14 and only namespaced providers will be supported.

The AWS provider was previously namespaced but in a way that was distinct from the new namespacing method. As such, how it is imported has changed to align with the new namespacing model. Previously the AWS provider could be imported in 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 namespacing model, it will now need 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 0.14 release is scheduled for a mid-November 2022 release. Kwon shares that the release "will focus primarily on quality-of-life improvements that make it easier to use pre-built providers." The team found that pre-built providers supply a better developer experience than locally generating them using cdktf get.

More information about the contents of the release can be found in the blog post and upgrade guide. Hashicorp hosts community office hours and has a discussion forum for questions. The CDK for Terraform Learn Guide is recommended for users new to the CDK.

About the Author

Rate this Article

Adoption
Style

BT