
What is Cloud Development Kit for Terraform (CDKTF)?
CDKTF allows users to produce Terraform configuration using programming languages such as C#, Python, TypeScript, Java, or Go. Users can use all Terraform providers and modules with CDKTF.
CDKTF uses the Cloud Development Kit from AWS, which provides a set of language-native frameworks for defining infrastructure, and adapters that let underlying provisioning tools use those definitions.
Prerequisites
To use CDKTF, you need:
- The Terraform CLI (1.1+).
- Node.js and npm v16+.
To follow this demo, you also need:
- GCP Project
Install CDKTF
Use npm to install CDFTF like below:
npm install --global cdktf-cli@latest
see the installation command output below:

Verify the installation using “cdktf –help” command

Now we are going to create a GCP BigQuery dataset with CDKTF using Python.
Create and initialize the project for CDKTF
Let’s create directory for the project and navigate to the directory.
$ mkdir devops-counsel-cdktf-demo
$ cd devops-counsel-cdktf-demo/
Inside the directory, initialize CDKTF with Python template for Python language and the --local
flag prevent CDKTF from using Terraform Cloud.
CDKTF prompts you for a project name and description. I used default values.
$ cdktf init --template=python --local
The above “cdktf init” command creates following files in project directory.
cloudshell:~/devops-counsel-cdktf-demo$ ls
cdktf.json help main.py Pipfile
Now, install Google Cloud provider for CDKTF python package
pip install cdktf-cdktf-provider-google
Python code for creating a BigQuery Dataset.
Replace main.py content with following code to create BigQuery dataset called cdkdemo_dataset
#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
from imports.google import (
GoogleProvider,
BigqueryDataset
)
class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)
GoogleProvider(self, id="bigquery-tf", region="us-central1", project="devops-counsel-demo")
cdkdemoBQDataset = BigqueryDataset(self, "cdkdemo", dataset_id="cdkdemo")
TerraformOutput(self, "bigquery-dataset-id", value=cdkdemoBQDataset.id)
app = App()
MyStack(app, "devops-counsel-cdktf-demo")
app.synth()
Add google-cloud provider “google@~> 3.3” to terraformProviders list in cdktf.json file like below.
cloudshell:~/devops-counsel-cdktf-demo$ cat cdktf.json
{
"language": "python",
"app": "pipenv run python main.py",
"projectId": "8209d1e4-55b1-4c58-b165-abf13f3583b5",
"sendCrashReports": "false",
"terraformProviders": ["google@~> 3.3"],
"terraformModules": [],
"codeMakerOutput": "imports",
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
Now run “cdktf get” to Generate CDK Constructs for GCP Terraform provider and modules, they will be stored under imports directory.
cloudshell:~/devops-counsel-cdktf-demo$ cdktf get
Generated python constructs in the output directory: imports
cloudshell:~/devops-counsel-cdktf-demo$
cloudshell:~/devops-counsel-cdktf-demo$ ls
cdktf.json help imports main.py Pipfile
Run “cdktf deploy” to deploy the dataset using the Python code in main.py file
$ cdktf apply
you can see terraform state file after apply has been finished successfully.
cloudshell:~/devops-counsel-cdktf-demo$ ls
cdktf.json cdktf.out help imports main.py Pipfile terraform.devops-counsel-cdktf-demo.tfstate
Deleting BigQuery Dataset using destroy
Run “cdftf destroy” to delete the BigQuery dataset that we have created earlier.
$ cdktf destroy
Conclusion
In this demo, I have installed CDKTF cli and initialized CDKTF project and deployed a simple BigQuery dataset using Python language.
I have also covered few CDKTF cli commands.
You can find more information on CDKTF in terraform official documentation.
For more on Terraform: