terraform

Announcing CDK for Terraform 0.4

The Cloud Development Kit for Terraform 0.4 introduces experimental Go language support, asset construct, and Terraform Cloud integration.

We are excited to announce the release of Cloud Development Kit (CDK) for Terraform 0.4. CDK for Terraform provides the ability to write Terraform configurations in C#, Python, TypeScript, and Java, using all existing Terraform providers and Terraform modules. CDK for Terraform 0.4 adds experimental Go support. You can download the new release by following the getting started guide in the programming language of your choice.

Key improvements in 0.4 include:

  • Go language support: Use experimental support for the Go language to create Terraform configurations.
  • Asset construct: Simplify workflows that require assets to be deployed, such as deploying serverless functions.
  • Improved Terraform Cloud integration: Output from Terraform Cloud is streamed rather than displayed at the end of the run.

»Go Support

Support for the Go language has been one of the top requests from users. The jsii library, which we use to generate language bindings between TypeScript and each of the supported languages, recently added experimental Go support. Building on this, we’re excited to announce experimental Go support in CDK for Terraform: you can now define infrastructure with Terraform providers using Go!

package main
 
import (
	"github.com/hashicorp/terraform-cdk/examples/go/aws/generated/aws_eks_module"
	"github.com/hashicorp/terraform-cdk/examples/go/aws/generated/hashicorp/aws"
 
	"github.com/aws/constructs-go/constructs/v3"
	"github.com/aws/jsii-runtime-go"
	"github.com/hashicorp/terraform-cdk-go/cdktf"
)
 
func NewExampleCdktfGoAwsStack(scope constructs.Construct, id string) cdktf.TerraformStack {
	stack := cdktf.NewTerraformStack(scope, &id)
 
	aws.NewAwsProvider(stack, jsii.String("aws"), &aws.AwsProviderConfig{
		Region: jsii.String("us-east-1"),
	})
 
	aws.NewInstance(stack, jsii.String("Hello"), &aws.InstanceConfig{
		Ami:          jsii.String("ami-2757f631"),
		InstanceType: jsii.String("t2.micro"),
		Tags: &map[string]*string{
			"environment": jsii.String("development"),
		},
	})
 
	aws_eks_module.NewAwsEksModule(stack, jsii.String("EKS"), &aws_eks_module.AwsEksModuleOptions{
		ClusterName:    jsii.String("my-eks"),
		Subnets:        jsii.Strings("a", "b"),
		VpcId:          jsii.String("id"),
		ClusterVersion: jsii.String("1.17"),
	})
 
	return stack
}
 
func main() {
	app := cdktf.NewApp(nil)
 
	NewExampleCdktfGoAwsStack(app, "ExampleCdktfGoAwsStack")
 
	app.Synth()
}
 

To try out writing infrastructure as code in Go, try our Go Getting Started guide. Note that this feature is still considered experimental, therefore it should not be used in production, and you may see changes to Go syntax. Please share your experiences with us by posting to the CDK for Terraform Discuss forum.

»Asset Construct

Deploying files or folders is a common part of many workflows, such as provisioning serverless applications. The new asset construct makes it easy to reference files and folders that need to be deployed alongside newly provisioned resources.

Here is a TypeScript example:

import * as path from "path";
import { Construct } from "constructs";
import { App, TerraformStack, TerraformAsset, AssetType } from "cdktf";
import { AwsProvider } from "./.gen/providers/aws/aws-provider";
import { S3BucketObject } from "./.gen/providers/aws/s3-bucket-object";
import { S3Bucket } from "./.gen/providers/aws/s3-bucket";
 
 
class MyStack extends TerraformStack {
  constructor(scope: Construct, name: string) {
    super(scope, name);
 
    new AwsProvider(this, "provider", {
      region: "us-west-2",
    });
 
    const bucket = new S3Bucket(this, "bucket", {
      bucket: "demo",
    });
 
    const asset = new TerraformAsset(this, "lambda-asset", {
      path: path.resolve(__dirname, "../lambda"),
      type: AssetType.ARCHIVE, // if left empty it infers directory and file
    });
 
    new S3BucketObject(this, "lambda-archive", {
      bucket: bucket.bucket,
      key: asset.fileName,
      source: asset.path,
    });
  }
}
 
 
const app = new App();
new MyStack(app, "demo");
app.synth();

The CDK for Terraform project and team have grown significantly since its 0.1 launch in January. Ansgar Mertens and Daniel Schmidt have recently joined the team, and with more people on board we’ve been able to expand the feature set:

  • Full Terraform module support allows HCL Terraform modules to be sourced from all the same sources as Terraform Core.
  • Multiple stacks were added in CDK for Terraform 0.3. Applications can be logically divided into stacks that represent:
    • Tiers: such as application and persistence
    • Stages: such as development and production, and
    • Regions

Each stack can be destroyed separately, and state is stored independently.

The CDK for Terraform changelog contains a comprehensive list of enhancements and bug fixes.

»Try CDK for Terraform

Whether you’re experimenting or actively using CDK for Terraform, we’d love to hear from you directly. Bring your questions to Terraform Community Office Hours on June 3rd, drop notes in the discussion forum, and report any issues you encounter in our issue tracker. We’re particularly looking for feedback on the new, experimental Go support, so please reach out!

We have language-specific guides and documentation in our GitHub repository, but if you’re new to the project, the CDK for Terraform Learn Guide is the best way to get started.


Sign up for the latest HashiCorp news

By submitting this form, you acknowledge and agree that HashiCorp will process your personal information in accordance with the Privacy Policy.