
- A practical problem that CDKTF can solve
- Prerequisites and essential tools for setting up your environment
- Creating a new CDKTF project from scratch
- An introduction to the CDKTF CLI and core commands

Problem Definition: Automating Local Project Setups
Meet Arthur, a developer who frequently starts new projects. Arthur is frustrated with repetitive tasks such as creating project folders and adding boilerplate files like.gitignore, package.json, and README. In every new Node.js project, Arthur needs to:
- Create a
.gitignorefile - Generate a
package.jsonfile containing the project name and standard boilerplate - Create a
READMEfile with project details






Setup and Prerequisites
Before you can start using CDKTF with TypeScript, ensure that you have the following prerequisites installed:- Node.js: Needed to run TypeScript and manage packages.

- Terraform: Install Terraform using appropriate commands (e.g., via Homebrew). Updating Homebrew before installation is recommended to get the latest version.
- CDKTF CLI: Install the CDKTF CLI globally using npm. Verify the installation with these commands:
Note: Installing CDKTF CLI
Run the following commands in your terminal:
Creating a New CDKTF Project
To create a new CDKTF project, navigate to an empty directory in your terminal and run:tsconfig.json, and installing necessary dependencies. A typical tsconfig.json file may resemble:
package.json will also include important dependencies and devDependencies:
Switching to Yarn (Optional)
If you prefer Yarn for its speed and modern tooling, you can switch by running:package-lock.json file and create a new yarn.lock file:
.gitignore file to exclude Yarn-specific files. If your IDE encounters package recognition issues, refreshing your workspace or reloading the browser window can help.
Here is a sample main file (main.ts) that is auto-generated during the CDKTF project setup:
Installing the CDKTF CLI as a Dev Dependency (Optional)
To pin the version of the CDKTF CLI for your CI/CD pipelines, you can also install it locally:Exploring the CDKTF Project Structure
After project initialization, you’ll notice several key files and directories:-
main.ts
Imports CDKTF constructs to define your infrastructure. AnAppis created and a custom stack (extendingTerraformStack) is added. -
cdktf.json
This configuration file specifies how your CDKTF application is executed. For example:If you prefer using Yarn, change the app command to"yarn ts-node main.ts". -
Terraform Output Synthesis
Runningapp.synth()inmain.tssynthesizes your TypeScript code into Terraform configuration files (typically in a.tf.jsonformat), which are stored in thecdktf.outdirectory.
Synthesizing and Deploying with CDKTF
To generate Terraform configuration files from your CDKTF application, run:cdktf.out) that contains the Terraform configuration for your stack (in this example, named “code”).
To deploy your infrastructure, execute:
terraform apply. You will be prompted to confirm the changes. After deployment, Terraform creates a state file locally. A typical local state file may look like:
Summary
In this guide, we explored the following key topics:- Problem definition using Arthur’s example of automating local project setups
- Essential prerequisites and tools for working with CDKTF
- Creating a new CDKTF project from scratch using TypeScript
- An introduction to the CDKTF CLI and its commands such as synth and deploy
