CDK for Terraform 0.3
Tags: terraform, cdk, cdktf, iac
Published: 2021-04-22 13:29:50

The [CDK (Cloud Development Kit) for Terraform](https://cdk.tf) allows developers to use familiar programming languages to define cloud infrastructure and provision it through HashiCorp Terraform. 

Since the [0.2](https://dev.to/skorfmann/cdk-for-terraform-0-2-3b71) release roughly a month ago, by far the biggest change was the onboarding of a newly hired full-time engineer. I'm  super happy that [Ansgar Mertens](https://twitter.com/amrtns) joined our team and am delighted to see the positive impact he already had on the project. 

## CDK for Terraform 0.3

We released [CDK for Terraform 0.3](https://github.com/hashicorp/terraform-cdk/releases/tag/v0.3.0) recently. Besides from various smaller and bigger improvements, there are two major new features: Remote templates and multiple stacks.

### Remote Templates

The `cdktf-cli` ships with a basic [template](https://github.com/hashicorp/terraform-cdk/tree/69e077545f82dd9ee199d8da45ba84503e59da2d/packages/cdktf-cli/templates) for each target language. These templates are the blueprint for the project which gets created by running `cdktf init`, e.g. `cdktf init --template typescript`.   

With [remote templates](https://github.com/hashicorp/terraform-cdk/blob/69e077545f82dd9ee199d8da45ba84503e59da2d/docs/working-with-cdk-for-terraform/remote-templates.md), users can now bring their own template. This enables customized starting points for `cdktf` projects tailored to the requirements of individual users and organizations.

### Multiple Stacks

Up until CDK for Terraform version `0.2` only a single stack per application was supported. Starting with version `0.3`, we're now enabling users to model their infrastructure in multiple stacks.

One stack is a collection of infrastructure which will be synthesized as a dedicated Terraform configuration. In comparison to the Terraform CLI, a Stack is equal to a dedicated working directory. Stacks are therefore a boundary to separate state within your infrastructure setup. This can be leveraged to separate a `cdktf` app in several layers (e.g. Network, Data, Compute), or to manage different versions of the same stack like in the following example. 

```typescript
import { Construct } from "constructs";
import { App, TerraformStack } from "cdktf";
import { AwsProvider, Instance } from "@cdktf/provider-aws";

interface MyStackConfig {
  environment: string;
  region?: string;
}

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string, config: MyStackConfig) {
    super(scope, id);

    const { region = 'us-east-1' } = config

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

    new Instance(this, "Hello", {
      ami: "ami-2757f631",
      instanceType: "t2.micro",
      tags: {
        environment: config.environment
      }
    });
  }
}

const app = new App();
new MyStack(app, "multiple-stacks-dev", { environment: 'dev' });
new MyStack(app, "multiple-stacks-staging", { environment: 'staging' });
new MyStack(app, "multiple-stacks-production-us", { environment: 'production', region: 'us-east-1' });
new MyStack(app, "multiple-stacks-production-eu", { environment: 'production', region: 'eu-central-1' });
app.synth();
```

There are two limitations to using multiple stacks at the moment:

- Operations such as `diff`, `deploy` and `destroy` are limited to one stack at a time for now - See this [issue](https://github.com/hashicorp/terraform-cdk/issues/650)
- Referencing a resource across stacks can't be done automatically, but has to be built intentionally by the user - See this [issue](https://github.com/hashicorp/terraform-cdk/issues/651)

Both limitations will be addressed in future releases. 

## What's next

Speaking of future releases, here's a [rough plan](https://github.com/hashicorp/terraform-cdk/milestone/8) what we'll be working on next. This includes the highest voted issue - Support for [Go](https://github.com/hashicorp/terraform-cdk/issues/368).

In the meantime, please checkout what we've built so far and make yourself [familiar](https://learn.hashicorp.com/tutorials/terraform/cdktf?in=terraform/cdktf) with CDK for Terraform :) 

{% github hashicorp/terraform-cdk %}