AWS Developer Tools Blog

Introducing the Cloud Development Kit for Terraform (Preview)

Infrastructure as Code (IaC) is a fundamental component of modern DevOps practices because it enables you to deploy any version of your application infrastructure at will, and facilitates the full lifecycle management of all the resources required to run and monitor your application. Organizations who have adopted DevOps practices often deploy hundreds or even thousands of changes to production a day, allowing them to deliver software faster, cheaper, and with lower risk.

When you explore the IaC options available today, you quickly discover customers have many choices, and two of the most popular for deploying infrastructure to AWS are CloudFormation, a service native to AWS, and Terraform, an open-source offering from HashiCorp. HashiCorp is an AWS Partner Network (APN) Advanced Technology Partner and member of the AWS DevOps Competency, and Terraform is a widely used tool that allows you to create, update, and version your infrastructure. According to GitHub Octoverse, HashiCorp Configuration Language (HCL) as one of the fastest growing languages over the past several years.

CloudFormation YAML and Terraform HCL are popular IaC languages and the right fit for many customers’ use cases, however we often hear other customers say they want to define and provision infrastructure with the same familiar programming languages used to code their applications, rather than needing to learn a new domain specific language. The AWS Developer Tools team responded with the AWS CDK in 2019 for CloudFormation, and now, AWS and HashiCorp are proud to announce that we’re bringing the CDK to Terraform.

The Cloud Development Kit for Terraform

Today, we’d like to tell you more about the developer preview of the Cloud Development Kit for Terraform, or cdktf, that lets you define application infrastructure with familiar programming languages, while leveraging the hundreds of providers and thousands of module definitions provided by Terraform and the Terraform community. The CDK for Terraform preview is initially available in TypeScript and Python, with other languages planned in the future.

The AWS Cloud Development Kit (CDK) and HashiCorp Terraform teams collaborated to create this new project by leveraging two key technologies of the AWS CDK: the CDK construct programming model, and the javascript interoperability interface, or jsii. The CDK construct programming model is a set of language native frameworks for defining infrastructure resources and adaptors to generate configuration files for an underlying provisioning engine. The jsii allows code in any supported language to naturally interact with JavaScript classes, enabling the delivery of libraries in multiple programming languages, all from a single codebase. Using these components, the AWS CDK generates CloudFormation configuration from code written in TypeScript, JavaScript, Python, Java, or C#. Similarly, the CDK for Terraform generates Terraform configuration to enable provisioning with the Terraform platform.

CDK for Terraform code sample

To better illustrate the developer experience using the CDK for Terraform, consider the following example. This example creates a new Terraform application called ‘hello-terraform‘, that contains a single stack named HelloTerraform. Within the HelloTerraform stack, the AWS provider is used to define CDK constructs to provision a EC2 instance. When this code is run, it produces a Terraform JSON configuration file that you can use to run a ‘terraform plan’ , ‘terraform apply’ or use the cdktf-cli to run ‘cdktf deploy’.

import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import { AwsProvider } from './.gen/providers/aws';
import { Instance } from './.gen/providers/aws/instance';
 
class HelloTerraform extends TerraformStack {
 constructor(scope: Construct, name: string) {
   super(scope, name);
 
   new AwsProvider(this, 'aws', {
     region: 'us-east-1'
   });
 
   new Instance(this, 'Hello', {
     ami: "ami-2757f631",
     instanceType: "t2.micro"
   });
 }
}
 
const app = new App();
new HelloTerraform(app, 'hello-terraform');
app.synth();

This code when synthesized will produce the following Terraform JSON configuration.

{
 "//": {
   "metadata": {
     "version": "0.0.10",
     "stackName": "hello-terraform"
   }
 },
 "terraform": {
   "required_providers": {
     "aws": "~> 2.0"
   }
 },
 "provider": {
   "aws": [
     {
       "region": "us-east-1"
     }
   ]
 },
 "resource": {
   "aws_instance": {
     "helloterraform_Hello_19940A68": {
       "ami": "ami-2757f631",
       "instance_type": "t2.micro",
       "//": {
         "metadata": {
           "path": "hello-terraform/Hello",
           "uniqueId": "helloterraform_Hello_19940A68",
           "stackTrace": [...
           ]

For an in-depth tutorial of using CDK for Terraform, read the HashiCorp blog post.

HashiCorp Terraform as a Platform for CDK Applications

HashiCorp Terraform follows an Infrastructure as Code approach and is extensible to support many providers of cloud infrastructure and software services.

Terraform As A Platform

Terraform as a platform with JSON, HCL, Kubernetes, and CDK for Terraform interfaces

Historically Terraform has supported HashiCorp Configuration Language (HCL) and JSON. While HCL is meant to be read and written by people, JSON can be machine-generated and consumed for programmatic interaction with the platform. Also, Kubernetes Custom Resource Definitions (CRDs) can be used to provision resources via the Terraform platform. Now, with the introduction of the CDK for Terraform, programming languages such as Python and TypeScript can be used to generate Terraform JSON configuration that is provisioned using Terraform.

Here are some additional things you need to know about CDK for Terraform:

Works with existing providers and modules. cdktf-cli includes a helpful CLI tool that lets you import anything hosted in the Terraform Registry into your project, allowing you to leverage any of the Terraform resource type providers or common infrastructure configuration modules.

Keep your current developer workflow. With the cdktf-cli, the core Terraform workflow remains the same, including the ability to plan changes before applying. Additionally, cdktf-cli supports commands like `cdktf diff` and `cdktf deploy` that are similar for AWS CDK users.

Familiar programming language to declarative state. cdktf code is written using familiar programming languages but produces your desired state as standard JSON. This means you can enjoy the expressiveness and power of programming languages without compromising on the robustness of the declarative desired state approach.

Language support. cdktf lets you define IaC using TypeScript and Python, with support for more languages coming in the future.

Open source. cdktf is an open source project and we can’t wait for the contributions from the community. If you would like to see a feature for the CDK for Terraform, please review existing GitHub issues and upvote. If a feature does not exist in a GitHub issue, feel free to open a new issue. In addition to opening issues, you can contribute to the project by opening a pull request.

Project is in alpha. cdktf is in the early stages of development, and while we think it is ready for you to give it a try and let us know what you think, use it with care and at your own discretion.

Learn More

We are very excited about this project, and helping make life more productive and fun for developers. To see the CDK for Terraform in action, we encourage you to read the HashiCorp blog post, and follow the step-by-step tutorial.

For more information about other projects that leverage the CDK construct programming model, check out the AWS CDK for defining CloudFormation configuration and cdk8s for defining Kubernetes configuration.

Happy Infrastructure-as-coding!

Tirumarai Selvan

Chris Fife

Chris is a Senior Product Manager at AWS on the Developer Tools team that brings you the AWS SDKs, the AWS CLI, and is a humble custodian of the AWS Cloud Development Kit (CDK). He is especially interested in emerging technology and seeing science fiction become science fact. When not thinking about DevOps and Infrastructure as Code, Chris can be found hiking, biking, boarding, camping, or paddling with his wife and two kids around the Pacific Northwest.

Tirumarai Selvan

Anubhav Mishra

Anubhav Mishra is a Technical Advisor to the CTO at HashiCorp. He is passionate about Developer Advocacy and helping developers and operators do better. Previously, he worked at Hootsuite, where he created Atlantis – An Open Source project that helps teams collaborate on Infrastructure using Terraform. Anubhav loves working with distributed systems and exploring new technologies. He also loves open source software and is continuously finding ways to contribute to projects that excite him. That has led him to contribute to projects like Virtual Kubelet (a CNCF project) and dapr. He often speaks at conferences. In his free time, he DJs, makes music and plays football. He’s a huge Manchester United supporter.

The content and opinions in this post are those of the third-party author and AWS is not responsible for the content or accuracy of this post.