terraform

CDK for Terraform 0.9 Adds Cross-Stack References and More

Cloud Development Kit for Terraform 0.9 (CDKTF) enables cross-stack references and improves generated resources for providers and modules.

We are excited to announce the release of Cloud Development Kit for Terraform (CDKTF) 0.9. With CDK for Terraform, you can write Terraform configurations in your choice of C#, Python, TypeScript, or Java (with experimental support for Go), and still benefit from the full ecosystem of Terraform providers and modules.

Key improvements in CDK for Terraform 0.9 include:

  • Cross-stack references: Reference resources across stacks in your application to make use of data sources across multiple environments.
  • Improved resources for providers and modules: Resources generated for Terraform providers and modules now include better support for sets and lists.
  • CDKTF output and output-file option: Get Terraform outputs and save them to a structured JSON file for easy workflow integration.

»Cross-Stack References

CDKTF applications can contain multiple stacks that each represent a collection of infrastructure components, allowing you to separate state management for multiple environments. For example, you may want a separate configuration for development, testing, and production environments. With CDKTF 0.9, you can now use cross-stack references to reference a resource in a different stack from the one where it was created.

Previously, this could only be achieved manually using outputs and the remote-state data source. Easily referencing resources across stacks makes development quicker and reduces the risk of inconsistencies across environments.

In the example below, the AWS region that was defined in a different stack (where you defined VPC resources), is exposed and made available to be used by the current stack (where you are defining resources for your Docker backend).

import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider } from "./.gen/providers/aws";
import { Vpc } from "./my-aws-vpc-construct";
import { DockerBackend } from "./my-docker-backend-construct";

class VPCStack extends TerraformStack {
  public vpc: Vpc;
  constructor(scope: Construct, id: string, public region = "us-east-1") {
    super(scope, id);

    new AwsProvider(this, "aws", {
      region,
    });

    this.vpc = new Vpc(this, "vpc", {});
  }
}

interface BackendStackConfig {
  region: string;
  vpcId: string;
  dockerImage: string;
}
class BackendStack extends TerraformStack {
  constructor(scope: Construct, id: string, config: BackendStackConfig) {
    super(scope, id);

    const { region, vpcId, dockerImage } = config;

    new AwsProvider(this, "aws", {
      region,
    });

    new DockerBackend(this, "docker-backend", {
      vpcId,
      dockerImage,
    });
  }
}

const app = new App();
const origin = new VPCStack(app, "origin-stack");
new BackendStack(app, "target-stack", {
  region: origin.region,
  vpcId: origin.vpc.id,
  dockerImage: "org/my-image:latest",
});

app.synth();
import { Construct } from "constructs";import { App, TerraformStack } from "cdktf";import { AwsProvider } from "./.gen/providers/aws";import { Vpc } from "./my-aws-vpc-construct";import { DockerBackend } from "./my-docker-backend-construct"; class VPCStack extends TerraformStack {  public vpc: Vpc;  constructor(scope: Construct, id: string, public region = "us-east-1") {    super(scope, id);     new AwsProvider(this, "aws", {      region,    });     this.vpc = new Vpc(this, "vpc", {});  }} interface BackendStackConfig {  region: string;  vpcId: string;  dockerImage: string;}class BackendStack extends TerraformStack {  constructor(scope: Construct, id: string, config: BackendStackConfig) {    super(scope, id);     const { region, vpcId, dockerImage } = config;     new AwsProvider(this, "aws", {      region,    });     new DockerBackend(this, "docker-backend", {      vpcId,      dockerImage,    });  }} const app = new App();const origin = new VPCStack(app, "origin-stack");new BackendStack(app, "target-stack", {  region: origin.region,  vpcId: origin.vpc.id,  dockerImage: "org/my-image:latest",}); app.synth();

»Improved Resources for Providers and Modules

CDKTF generates the required code bindings for the providers and modules that you define in cdktf.json. This allows you to define resources for that provider in your application. We discovered that the generated resources didn’t always work nicely in every supported programming language. The 0.9 release introduces two improvements to make the generated resources for providers and modules easier to work with.

»Working with Sets

Our community noticed that providers and modules that use sets were sometimes failing to generate code bindings, or that the sets were treated as arrays, making the returned resources difficult to access. This release provides helper methods for accessing individual items or all items as a list.

Note: This is an intermediate step until JSII has support for sets, which will allow us to model them properly in all target languages.

»CDKTF Output and Output-File Option

Previously, when you needed the outputs of your CDK application, you had to run Terraform output by hand; now the CDK has its own version of the ‘output’ command. To integrate better into your tooling, we added the –output-file flag for the deploy and output commands. The file that is written is structured like your CDKTF application, so it's predictable to work with.

»Other Improvements

The CDK for Terraform CHANGELOG contains a comprehensive list of additional enhancements and bug fixes. Please note that CDKTF 0.9 contains breaking changes that may require code updates. Please see the CHANGELOG and 0.9 Upgrade Guide for details.

»Try CDK for Terraform

If you’re new to the project, the tutorials for CDK for Terraform on HashiCorp Learn are the best way to get started, or dive deeper into our documentation beginning with this overview of CDKTF.

Whether you’re experimenting or actively using CDK for Terraform, we’d love to hear from you. Please file any bugs you encounter, let us know about your feature requests, and share other questions, thoughts, and experiences in the CDK for Terraform discussion forum.


Sign up for the latest HashiCorp news