Skip to main content
In this section we’ll cover essential CDKTF concepts: providers and resources, outputs, and constructs. We’ll continue Author’s hands-on journey with CDKTF (TypeScript) to automate local project scaffolding — creating common files like README.md, package.json, and .gitignore using the local provider.
A presentation slide titled "Text Files With CDKTF – Problem" showing an icon of a person at a computer with gears above it. The slide notes the author wants to automate creation of common files (README.md, package.json, .gitignore).

Quick checklist before you begin

  • Initialize a CDKTF TypeScript project (cdktf init --template="typescript"), then run:
    • yarn to install dependencies
    • yarn cdktf synth to verify synthesis
    • yarn cdktf deploy to apply changes

Minimal stack to verify compilation and synth

Start with a tiny stack that produces a Terraform output to confirm your CDKTF toolchain is working:

Add the local provider dependency

To use local file resources you must install the provider package:
Install @cdktf/provider-local so CDKTF can synthesize the corresponding Terraform provider block and the local_file resources.

Create local files using the local provider

The @cdktf/provider-local package exposes a LocalProvider and file.File resource (maps to Terraform’s local_file). The example below:
  • Initializes the local provider,
  • Computes a target base path relative to the folder where you run yarn cdktf deploy (using process.env.INIT_CWD and Node’s path),
  • Creates README.md at <projectDirectory>/<projectName>/README.md.
When you run yarn cdktf deploy, CDKTF will:
  1. Synthesize Terraform configuration (you’ll find generated files under cdktf.out/),
  2. Execute terraform init / plan / apply to create or update the local files.

Where Terraform state is stored

By default CDKTF (like Terraform CLI) uses a local backend unless you configure a remote backend. In the synthesized output you will find a JSON backend configuration that points to a local .tfstate file. Example:
If you need a remote state (e.g., S3, Terraform Cloud), configure the backend explicitly in your project.

Changing file content and lifecycle behavior

  • Updating the content of a local_file usually results in an in-place update (the provider updates the file’s contents).
  • If you want Terraform to ignore subsequent content changes (so updates in CDKTF don’t trigger applies), use the lifecycle.ignoreChanges attribute.
Change the content (simple example):
Ignore future content changes with lifecycle:
Using lifecycle.ignoreChanges prevents Terraform from reconciling the content property. Use it only when you intentionally want to decouple local file content management from Terraform updates.

Changing logical IDs and adding resources

  • Logical ID: the name you provide in code (for example 'readme-file') identifies the construct in the construct tree and influences the generated Terraform resource name. Renaming it (e.g., to 'readme-file-2') will cause CDKTF to treat the original resource as removed and the new one as added — Terraform will plan to destroy then create.
  • Adding more files is as simple as adding more file.File resources. Use JSON.stringify to write formatted JSON into files like package.json.
Example for package.json:

Key concepts at a glance

  1. yarn — install dependencies including @cdktf/provider-local
  2. yarn cdktf synth — confirm the Terraform configs CDKTF will generate
  3. yarn cdktf deploy — apply changes and create files on disk
  4. Inspect cdktf.out to view synthesized Terraform and backend configuration

References

This concludes the section on providers and resources.
A minimal three-step horizontal timeline with numbered circles: 01 (highlighted teal) labeled "Providers & Resources," 02 labeled "Outputs," and 03 labeled "Constructs." A thin gray line connects the circles and a small "© Copyright KodeKloud" appears in the corner.

Watch Video