DEV Community

Cover image for What Is Terraform CDK? - Terraform code in JavaScript
Raghav Mrituanjaya
Raghav Mrituanjaya

Posted on • Edited on • Originally published at thegogamicblog.xyz

What Is Terraform CDK? - Terraform code in JavaScript

What is Terraform🤔?

Terraform is an open-source infrastructure as a code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. It was developed by HashiCorp

What is Terraform CDK?

Terraform CDK is a tool developed by terraform developers which allows you to write your infrastructure in your favorite language

  • It supports TypeScript, Python, C#, Go(Beta), and Java Currently.

Getting started 😃

Before we get started install cdktf-cli by running npm i -g cdktf-cli@latest and then run cdktf init to initialize a broiler code for you. Choose the language and name as your choice. For this instance, I will be using typescript.

Terraform-CDK-Prebuilt-Providers

Let's add AWS providers and try to make an EC2 Instance. There are some prebuilt providers like the following for typescript. So in this case we install the AWS provider by running npm i @cdktf/provider-aws

Terraform-CDK-languages

If in case you are using any other language then you can add your providers to the terraformProviders field in the cdktf.json file and run cdktf get to install the constructs and convert them to your native language

{
"language": "typescript",
"app": "npm run --silent compile && node main.js",
"projectId": "5632caca-2a91-436c-963d-57dccfa2d7b0",
"terraformProviders": ["aws@~> 3.63"],
"terraformModules": [],
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
view raw cdktf.json hosted with ❤ by GitHub

Here's my Typescript Code

import { Construct } from 'constructs';
import { App, TerraformStack } from 'cdktf';
import { AwsProvider, EC2 } from '@cdktf/provider-aws';
class MyStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
// define resources here
new AwsProvider(this, 'aws', {
region: 'us-east-1',
accessKey: 'accessKey', // enter your access key here
secretKey: 'secretKey', // enter your secret key here
});
new EC2.Instance(this, 'ec2', {
instanceType: 't2.micro',
ami: 'ami-0b9c9f1d',
availabilityZone: 'us-east-1a',
keyName: 'my-key-pair',
});
}
}
const app = new App({
outdir: 'dist', // set output directory as your needs
skipValidation: false, // set to true if you want to skip validation
stackTraces: false, // set to true if you want to see stack traces
});
const stackName = 'my-stack'; // set stack name as your needs
new MyStack(app, stackName);
app.synth();
view raw main.ts hosted with ❤ by GitHub

Running cdktf synth will create a directory called cdktf.out where you will have your stack named folder which contains the terraform files.

Now you can run terraform init & terraform plan to initialize terraform and review your infrastructure resources and if you want to go ahead and apply the changes run terraform apply

Notes

  • You must have to terraform installed in order for cdktf to work. You can Download the latest version by visiting this link
  • If you guys want me write the same for other language do let me know in the comment section below.

Video Reference

Top comments (0)