DEV Community

loading...

CDK for Terraform 0.3

Sebastian Korfmann
Terraform CDK (https://cdk.tf) @ HashiCorp / AWS DevTools Hero / https://cdk.dev
・3 min read

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

CDK for Terraform

This experimental repository contains software which is still being developed and in the alpha testing stage. It is not ready for production use.

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

npm version PyPI version NuGet version Maven Central homebrew

Overview

CDK for Terraform apps are written in one of the supported programming languages and can leverage the entire ecosystem of Terraform modules and providers. The application synthesizes Terraform configuration in JSON. Terraform or the CDK for Terraform can then be used to deploy the application.

The CDK for Terraform project 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.

The project currently supports TypeScript, Python, Java, and C#.

Getting Started

Choose a language:

Discussion (0)