CDKTF Frequently Asked Questions
Tags: terraform, sre, devops, programming
Published: 2023-02-23 11:27:52

## CDKTF Frequently Asked Questions

### What does CDKTF mean?

CDKTF stands for *Cloud Development Kit for Terraform*.  The term *CDK* is used to describe tools which allow managing cloud infrastructure using common programming languages.

### What problem does CDKTF solve?

Developers can manage their infrastructure using familiar programming languages in place of the Terraform HCL language.  As well as using familiar programming languages, developers benefit from being able to use the ecosystem of their language of choice when developing infrastructure components.

CDKTF was initially developed in
[collaboration](https://aws.amazon.com/blogs/opensource/announcing-cdk-for-terraform-on-aws/) with AWS which had released [AWS CDK](https://aws.amazon.com/cdk/).  AWS CDK turned out to be popular and developers really liked being able to work in their language of choice.

AWS CDK and CDKTF are similar to Pulumi, which was developed with the idea of managing infrastructure using familiar programming languages.

Key differences between the three offerings are as follows:
- Pulumi has its own engine for planning and applying changes
- AWS CDK generates AWS Cloud Assembly for planning and applying changes
- CDKTF generates Terraform code (HCL) for planning and applying changes

### What languages does CDKTF support?

CDKTF can be written in the following languages:
- [TypeScript](https://developer.hashicorp.com/terraform/cdktf/api-reference/typescript)
- [Python](https://developer.hashicorp.com/terraform/cdktf/api-reference/python)
- [Java](https://developer.hashicorp.com/terraform/cdktf/api-reference/java)
- [C#](https://developer.hashicorp.com/terraform/cdktf/api-reference/csharp)
- [Go](https://developer.hashicorp.com/terraform/cdktf/api-reference/go)

### Is CDKTF production ready?

**Yes**.  On [Aug 1
2022](https://www.hashicorp.com/blog/cdk-for-terraform-now-generally-available), HashiCorp announced that CDKTF is Generally Available and ready for production usage.  CDKTF is still a maturing technology, however, you should feel confident using it in a production environment.

### What are the advantages of CDKTF?

- Developers who are familiar with programming languages but not HCL can contribute to infrastructure changes.
- Being able to express infrastructure in TypeScript, Python, Java, C#, or Go allows for leveraging the ecosystem of those languages.
- The development environments (editors, IDEs, language servers, testing frameworks) are very mature.
- Programming languages are more powerful than HCL.
- More people are familiar with programming languages than HCL.

### What are the disadvantages of CDKTF?

- CDKTF is still maturing.  The underlying engine which executes CDKTF is robust. One needs to have implementations of providers and resources in the CDKTF ecosystem, which is still growing.
- Documentation, especially around providers, is lacking.  One might have to read code in order to understand how to use a provider.
- CDKTF is complicated.  It requires converting code written in a programming language to HCL, which you could have written yourself.
- CDKTF also adds new concepts (for example "stacks"), making it not a direct one-to-one translation of Terraform.  This means those developers who are familiar with Terraform will need to learn CDKTF before they are effective.
- With great power comes great responsibility.  Programming languages are much more expressive than HCL.  While HCL has its limitations, these limitations also mean the code has a ceiling of complexity.  Programming languages do not have the same constraints.

### Should I use CDKTF?

Like most technical decisions: it depends.

Whether or not CDKTF is the right decision for you is something you need to determine.

Here are some factors you should consider:

1. Is using CDKTF going to make it easier for developers in my organization to contribute infrastructure changes?
2. Do I have the [innovation tokens](https://mcfunley.com/choose-boring-technology) to use a maturing technology?

CDKTF will continue to be developed and improved by HashiCorp, so if you like where it is now, it is probably a good decision.

### Does CDTKTF require Terraform Cloud?

No.  You can use CDKTF anywhere.

### What language is CDKTF implemented in?

The CDKTF CLI is implemented in TypeScript.  Providers and resources are implemented in TypeScript as well.  [jsii](https://aws.github.io/jsii/) is used to compile the providers and resources to the supported languages.

### How does CDKTF work?

The high-level process of how CDKTF works is as follows:

1. Developer writes code in their preferred programming language that CDKTF supports.
2. The code must create an instance of an "app".
3. The developer creates "stacks", "providers", and "resources" and adds them to the "app".  This creates an internal representation of the infrastructure which can be turned into something else using the `synth` function.
4. After constructing the infrastructure objects, at the end the developer must call the `synth` function.
5. The user then executes `cdktf diff`, `cdktf deploy`, or `cdktf synth`.
6. The CDKTF CLI runs the code the developer wrote and the call to `synth` knows how to convert the app (which has the stack and resources and providers added to it) to the JSON representation of HCL.
7. The CDKTF CLI can then run `terraform plan` or `terraform apply` on the HCL.

It is possible that `cdktf diff` and `cdktf deploy` could perform the translation to Terraform HCL and execution in-memory, however `cdktf synth` will always produce Terraform HCL in a directory that you can manually run the Terraform CLI against.

Considering the following CDKTF code in TypeScript:

```
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import * as Null from './.gen/providers/null';

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new Null.provider.NullProvider(this, 'test-provider');
    new Null.resource.Resource(this, 'test');
  }
}

const app = new App();
new MyStack(app, "cdktf-stack1");
app.synth();
```

On the last line, `app.synth()`, the entire app has been created and all providers and resources have been added to the stack.  The `synth` call can then
produce Terraform HCL files and the CDKTF CLI can execute `terraform plan` or `terraform apply`.

### How can I convert Terraform code to CDKTF?

The CDKTF CLI can convert Terraform files using [cdktf
convert](https://developer.hashicorp.com/terraform/cdktf/cli-reference/commands#convert).

### What is an app?

CDKTF code constructs an object called "app".  This represents all of the infrastructure that will be generated in CDKTF.  Stacks will be added to the app.  And providers and resources will be added to the stack.

### What is a stack?

Stacks represent a collection of infrastructure configuration that will be managed.  You can think of them as somewhere between Terraform modules and workspaces.  You might use a stack to represent the infrastructure in an AWS region and then create an instance of that stack per region.  For example:

```
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string, region: string) {
    super(scope, id);

    // define resources here
  }
}

const app = new App();
new MyStack(app, "cdktf-stack1", "us-east-1");
new MyStack(app, "cdtff-stack2", "us-west-1");
app.synth();
```

### Where can I learn more about CDKTF?

- [CDK for Terraform Official Documentation](https://developer.hashicorp.com/terraform/cdktf)
- [`hashicorp/terraform-cdk repository`](https://github.com/hashicorp/terraform-cdk)