
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 (hereConstruct).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.
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 thelocal 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
readonlyclass properties.
constructs/project-folder.ts:
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:

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.
Links and references
- CDK for Terraform: https://developer.hashicorp.com/terraform/cdktf
- Constructs programming model: https://github.com/aws/constructs
- Terraform modules (HCL): https://www.terraform.io/language/modules
- Node.js path module (used for
path.join): https://nodejs.org/api/path.html