Blog

How to use AWS CDK for Terraform

Oct 15, 2021 | Announcements, Migration, MSP

nClouds’ engineers have extensive experience with CloudFormation and infrastructure as code (IaC), and we’ve found that AWS CDK is an invaluable tool as we build solutions for our clients. We used to think in terms of AWS Cloud Development Kit (AWS CDK) vs. Terraform for provisioning infrastructure — but with AWS CDK for Terraform (cdktf), we get the best of both and use IaC in new ways. This gives you options in terms of how you define and provision your infrastructure. 

If you have used AWS CDK before with AWS CloudFormation, this tutorial will feel super familiar and basically uses the same commands. However, the big benefit is that the cdktf tool lets you use other languages like Python, Java, Go, and others instead of having to learn specific languages to start using AWS CDK.

What is AWS CDK for Terraform?

AWS CDK is an open-source software development framework to define your cloud application resources. You may already be familiar with CloudFormation, which allows you to create templates through formatted text files to build out your AWS resources with JSON or YAML. If you use CloudFormation, you’ve probably run into a broken CloudFormation stack that doesn’t want to roll back or is in a failed state. And then, you need to do extra work to get it back to a stable situation so you can update or delete it.

AWS CDK for Terraform, though, uses HashiCorp Configuration Language (HCL), which is designed to be a bit more human-friendly. This is one of the reasons some developers prefer Terraform. With cdktf, you get increased provisioning options.

How AWS CDK for Terraform works

When you look at the technical demo video, you’ll see that when you run the cdktf deploy command, what you see is the Terraform output. You can run the cdktf synth (which is short for synthesize) command, which will render your configuration into Terraform.

Because Terraform backs this CDK, you won’t run into as many problems with broken stacks as you would with the CloudFormation version.

Each has some advantages – you can make your own judgment once you use either one or both of them. That said, let’s move on to the step-by-step tutorial.

Setting up an example with TypeScript

For this tutorial, we chose TypeScript, although AWS CDK for Terraform supports other languages, including Python, Java, Go, C#, and others. This example will take a TypeScript project and render Terraform output. Through this, you can see the structure of how AWS CDK for Terraform looks and what it generates.

Creating a Docker image and container with cdktf synth:

1. First, you’ll create a Docker image and Docker container: Everything is coded in TypeScript, and you’ll see all the files when you choose a project, but I added our Docker image and container to the main TypeScript file.

2. Run the cdktf synth command: Next, you’ll begin rendering the code into Terraform and generating the code. Your output will be in full JSON syntax – this is the not-user-friendly part, but this is automated and you won’t be modifying these files.

3. Run cdktf deploy: By running this command next, you’ll automatically generate the Terraform code output that you need.

4. Type “yes” in response to the Terraform confirmation: Terraform will ask you, “Do you want to perform these actions?” and entering “yes” will create the Docker image and Docker container.

Using AWS resources:

1. Create a new folder: In this example, I created a new folder named cdktf-aws-demo.

2. Run the cdktf init command: Running this command, you can choose a template with ‑‑template to set the language you want to use. I’m going to open with TypeScript again and keep the project local, so I set ‑‑local also. This will create all the scaffolding for the project here, using the folder name by default. Since I’m asked for a description, I’ll use Friday demo cdktf.

3. Define AWS provider and define resources: If you navigate to your folder (cdktf-aws-demo in our example), you’ll see that we can define our resources on the main TypeScript file. In cdktf.json, we already have an array for Terraform providers, so we can add our AWS provider there by adding hashicorp/aws and the version constraint.

4. Run cdktf get: Now, with my provider defined, I can have Terraform work on my TypeScript file. I need to import the provider I just installed and create an instance, so I’ll import this package.

5. Using the AWS provider, declare an instance: Verify the Region and AMI – check your information to ensure you have the correct Region and AMI included. At this point, you’ll want to add the code you need to deploy an instance. At the bottom, add the Terraform output. We want the public IP address of our instance.

6. Run cdktf synth: At this point, you can synth to render the files. Once this is done, I should see my cdktf.out file. Synth renders this Terraform code in JSON.

7. Export and deploy: I’ll run the command export AWS_PROFILE=nclouds to set up the profile, then run cdktf deploy. AWS CDK for Terraform will then ask you for confirmation that you want to deploy – type “yes” to keep going. You can use cdktf destroy after deploying the instance defined here.

Using AWS CDK for Terraform

Many of our customers already use CloudFormation but want to start provisioning infrastructure using the programming languages they already use in coding applications. AWS CDK for Terraform lets you do just that. You can use TypeScript, Python, Go, Java, JavaScript, C# and others to create CloudFormation configurations.

While AWS CDK makes it easier to build cloud applications, if you don’t implement it correctly, you’ll face challenges keeping the infrastructure up-to-date and applying changes, particularly when managing multiple applications, Regions, and environments. That’s why our clients rely on nClouds to apply our best practices — enhanced security, resource tagging, reusing code, using Compliance-as-Code and our proprietary nCodeLibrary —  when we implement AWS CDK in their infrastructure.

Want to learn more about AWS CDK? Check it out.

Blog Post | What Is AWS CDK, and Why Should Your DevOps Teams Use It?

Do you need help with your AWS infrastructure? Connect with the nClouds team. We can help free up your engineers for high-value, innovative projects to achieve your technical and business goals on AWS. Contact Us.

GET SUBSCRIBED

nClouds
nClouds is a cloud-native services company that helps organizations maximize site uptime, performance, stability, and support, bringing out the best of their people and technology using AWS