BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News CDK for Terraform Adds Go Support and Improves Asset Construction

CDK for Terraform Adds Go Support and Improves Asset Construction

This item in japanese

Hashicorp recently released version 0.4 of their CDK for Terraform. This release adds experimental support for using Go to write Terraform configurations. Also included are enhanced support for Terraform modules and asset construction.

The CDK allows for writing Terraform configurations in a number of programming languages including C#, Python, TypeScripts, and Java. It includes support for all existing Terraform providers and modules.

The underlying jsii library that is used to generate language bindings added experimental Go support in a recent release. This allowed the Hashicorp team to extend that and introduce Go support within the CDK. For example, an AWS EC2 instance could be declared using the CDK in Go as follows:

func CreateStack(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"),
    },
  })

  return stack
}

By running cdktf synth, the following output will be produced:

{
  "terraform": {
    "required_providers": {
      "aws": {
        "version": "~> 3.40.0",
        "source": "hashicorp/aws"
      }
    }
  },
  "provider": {
    "aws": [
      {
        "region": "us-east-1"
      }
    ]
  },
  "resource": {
    "aws_instance": {
      "Hello": {
        "ami": "ami-2757f631",
        "instance_type": "t2.micro",
        "tags": {
          "environment": "development"
        }
      }
    }
  }
}

The generated Terraform can now be deployed either via usual terraform commands or through the CDK with cdktf deploy. The team notes that the support for Go is experimental and should not be used within production workloads. In addition, the AWS provider currently requires approximately 6 GB of memory for processing.

The new asset construct simplifies referencing files and folders that need to be deployed alongside the provisioned resources. The recommended use cases include deploying static local files and copying over generated zip files for Lambda functions. In this TypeScript example, an asset is created that references a directory containing the code package for a Lambda function:

const asset = new TerraformAsset(this, "lambda-asset", {
  path: path.resolve(__dirname, "../lambda"),
  type: AssetType.ARCHIVE,
});

Pre-built providers are now being published in language-native repositories such as PyPI, NPM, Nuget, and Maven Central. A full list of available providers is available in the GitHub repo. Providers and modules hosted within the terraform registry can be imported into the project by adding them to the cdktf.json and running cdktf get. For other modules, TerraformHclModule can be leveraged, however it does not have type safe inputs or outputs.

More information about the contents of the release can be found within the changelog. Hashicorp hosts community office hours and has a discussion forum for questions. The CDK for Terraform Learn Guide is recommended for users new to the CDK.

Rate this Article

Adoption
Style

BT