11 Alternative interfaces

This chapter covers

  • Controlling Terraform from another language
  • Breaking down the Terraform machine-readable UI into usable data structures
  • Creating hooks to respond to Terraform streaming events as they occur
  • Generating valid Terraform using JSON
  • Using the CDK for Terraform project to generate valid Terraform from other languages

Terraform is both a tool and a language. The tool is the Terraform or OpenTofu binary itself: an engine and command-line interface (CLI) application that is used to process code written in the Terraform language. The language, as we’ve seen throughout this book, is built on top of HashiCorp Configuration Language (HCL) and is populated with resources, data sources, and functions that are added with the use of providers.

What if you wanted to use Terraform without Terraform? As weird as that may sound, there are a lot of use cases where that comes up. Some teams simply don’t want to learn Terraform and HCL, so they may want to develop their infrastructure as code (IaC) using a language they’re more familiar with. Other teams may have a need to automatically generate Terraform, which is easier to do with JSON than it is with HCL. On the other side of things, teams may want to access the Terraform engine itself using their own language, with the goal of building their own tools on top of Terraform.

11.1 Wrapping Terraform

11.1.1 JSON output and machine-readable UI

11.1.2 Initial Terraform client

11.1.3 Init

11.1.4 A detour into testing

11.1.5 Validating

11.1.6 State

11.1.7 Apply

11.1.8 Plan

11.1.9 Output

11.1.10 Using our library

11.2 Using JSON instead of HCL

11.2.1 JSON structure

11.2.2 Expressions and keywords

11.2.3 Comments

11.3 Cloud Development Kit for Terraform

11.3.1 Should I use CDKTF?

11.3.2 CDKTF setup

11.3.3 Apps, stacks, and resources