DEV Community

Sebastian Korfmann
Sebastian Korfmann

Posted on

CDK for Terraform 0.3

The CDK (Cloud Development Kit) for Terraform allows developers to use familiar programming languages to define cloud infrastructure and provision it through HashiCorp Terraform.

Since the 0.2 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 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 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 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, 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.

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();
Enter fullscreen mode Exit fullscreen mode

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
  • Referencing a resource across stacks can't be done automatically, but has to be built intentionally by the user - See this issue

Both limitations will be addressed in future releases.

What's next

Speaking of future releases, here's a rough plan what we'll be working on next. This includes the highest voted issue - Support for Go.

In the meantime, please checkout what we've built so far and make yourself familiar with CDK for Terraform :)

GitHub logo hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform

npm version PyPI version NuGet version Maven Central

CDK for Terraform

Cloud Development Kit for Terraform (CDKTF) allows you to use familiar programming languages to define cloud infrastructure and provision it through HashiCorp Terraform. This gives you access to the entire Terraform ecosystem without learning HashiCorp Configuration Language (HCL) and lets you leverage the power of your existing toolchain for testing, dependency management, etc.

We currently support TypeScript, Python, Java, C#, and Go.

terraform platform

CDKTF includes two packages:

  • cdktf-cli - A CLI that allows users to run commands to initialize, import, and synthesize CDK for Terraform applications.
  • cdktf - A library for defining Terraform resources using programming constructs.

Get Started

Choose a language:

Hands-on: Try the tutorials in the CDK for Terraform collection on HashiCorp Learn.

Documentation

Refer to the CDKTF documentation for more detail about how to build and manage CDKTF applications, including:

Top comments (0)