Skip to main content
In this lesson we cover how to define and capture outputs in CDK for Terraform (CDKTF). When your CDKTF resources produce values you want to inspect, pass between stacks, or use in automation, expose them as Terraform outputs by instantiating the TerraformOutput class — the CDKTF equivalent of an HCL output block.
A presentation slide titled "Working With Outputs in CDKTF – Problem" showing a stylized icon of a person at a monitor with code brackets. A grey caption reads, "Author needs to confirm the outputs and content of created files."

When to use outputs

Use Terraform outputs to:
  • Expose resource attributes for human inspection (printed after cdktf deploy).
  • Share data between stacks (e.g., export a value from one stack to reference in another).
  • Provide values to automation scripts or CI pipelines.
Common CDKTF pattern:
  • Create resources.
  • Store resource references in variables (so you can access attributes).
  • Define TerraformOutput instances after resource creation.

Quick example: expose a README file’s content

Create a Terraform output that exposes the content attribute of a local file resource:
Below is a compact, complete TypeScript CDKTF example that creates two local files and exports the README content as an output.
Define outputs after creating the resources they reference. Keep a reference to the resource (for example readmeFile) so you can access attributes like readmeFile.content when constructing TerraformOutput.

TerraformOutput quick reference

Example usage with optional properties:

Deploying and viewing outputs

When you run cdktf deploy, you’ll confirm the planned changes and, after apply completes, Terraform prints outputs. Example terminal session:
Now the README content is available as a Terraform output and can be:
  • Inspectable in the terminal,
  • Used by other stacks,
  • Consumed by automation or CI/CD pipelines.

References

A simple horizontal three-step infographic with numbered circles connected by a line: "01 Providers & Resources," a highlighted teal "02 Outputs" in the center, and "03 Constructs" on the right. A small © Copyright KodeKloud appears in the lower-left.

Watch Video