Skip to main content
This article explains how to create reusable constructs in CDK for Terraform (CDKTF). Constructs let you encapsulate resource creation and logic in reusable, programmatic units—similar in purpose to Terraform modules but with the power of TypeScript. Arthur wants to reuse his project setups, so he creates a ProjectFolder construct to encapsulate repetitive tasks and to group together the resources we’ve created so far into a reusable building block.
A slide titled "Creating Constructs in CDKTF – Solution" showing a stylized person and monitor with code, and the instruction "Create a construct ProjectFolder to handle repetitive tasks." A footer notes the Terraform equivalent: constructs → Terraform modules (HCL).

Why use constructs?

  • Reuse: package common patterns (folders, files, provider initialization) once and reuse across stacks and projects.
  • Composition: compose small constructs into larger systems.
  • Type-safety & programmability: use TypeScript for control flow, loops, and conditional logic that would be awkward in HCL modules.
  • Direct object references: CDKTF lets you expose resource objects directly (not just primitive outputs), enabling richer composition.

Minimal ProjectFolder construct (boilerplate)

The example below shows a minimal construct shell that illustrates common TypeScript patterns used in CDKTF constructs.
Key TypeScript notes:
  • extends: creates a class that inherits from a base class (here Construct).
  • super(scope, id): calls the parent class constructor to initialize inherited behavior.
  • readonly: marks a property as immutable after initialization.
  • Destructuring (const { projectName, projectDirectory } = props) is a concise shorthand for extracting properties from an object.
Place construct files in a constructs folder. When a file exports a single class, it’s common to name the file after that class (for example, project-folder.ts).

Example stack that uses the construct

This stack initializes the local provider, sets up base variables (project directory and name), and instantiates the ProjectFolder construct. It also demonstrates exposing a value from the construct as a Terraform output.

Move resource creation into the construct

The construct should:
  • Define the properties it needs (via ProjectFolderProps).
  • Create the resources it manages.
  • Expose any values or resource references the stack or other constructs might need by assigning them to readonly class properties.
Example constructs/project-folder.ts:
By assigning the resource to this.readmeFile (instead of a local const), the stack that instantiates this construct can reference projectFolder.readmeFile to access resource attributes (for example, its content). When the construct is instantiated in the stack (as shown earlier), you create a Terraform output from an attribute exposed by the construct:
A presentation slide titled "Creating Constructs in CDKTF – Solution" with an icon of a person at a monitor displaying code brackets. The slide includes the instruction: "Expose a read-only property from ProjectFolder construct."

Quick reference

Pattern benefits

  • Encapsulate resource creation in a reusable construct.
  • Expose meaningful, read-only properties (resource objects and attributes) for use by the stack or other constructs.
  • Keep stacks declarative while composing constructs programmatically.
  • Unlike HCL modules, CDKTF allows direct object references and richer composition patterns via TypeScript.
Best practice: Keep constructs small and focused. Expose only the properties that other stacks or constructs need to keep the API surface minimal and easier to maintain.

Watch Video