liveBook · Manning

HashiCorp Configuration Language

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

Terraform HCL is the Terraform-specific dialect of HCL, including Terraform's syntax, resources, and functions.

To describe infrastructure without scripting every deployment step, write the desired end state in HashiCorp Configuration Language (HCL). Terraform uses its own flavor of HCL, with Terraform-specific resources and functions. The format is designed to be readable by people who are new to it. section 1.2.1

Terraform reads configuration files to construct a dependency graph. Its usual workflow is init, plan, apply: obtain needed providers or modules, compare the desired configuration with the workspace, then carry out the plan. HCL describes what should exist; the plan determines the actions. section 1.7 section 11.2

Listing 5.6 Variable file with HCL
# Production Data                             #1
vpc           = "vpc-01234567890abcdef"       #2
num_instances = 2

A production variable file shows a narrower use of HCL: commented, key-equals-value assignments for a VPC identifier and instance count. Unlike a full configuration, a variable file has no blocks and accepts literal values only—not functions or transformations. section 5.4.3

Use a .tfvars file with a command such as terraform plan -var-file=production.tfvars, or name it terraform.tfvars or *.auto.tfvars for automatic loading. The extension matters: .tfvars.json denotes JSON instead. JSON is also an alternative for entire Terraform configurations, especially when software generates them; HCL remains the more readable choice for human-written files. section 5.4.3 section 11.2

HCL can also express native Terraform tests, avoiding a second testing language such as Go. Check version compatibility before relying on that framework: it first appeared in Terraform and OpenTofu v1.6 and cannot test older versions. section 9.1.3

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines Terraform's HCL dialect and details variable-file syntax and loading, including its literal-only restriction.
Examples
Shows a production .tfvars file containing a VPC identifier and instance count, alongside a JSON equivalent.
What it assumes
Introduces HCL as readable even without prior fluency and defines its declarative approach.
What you can do after
Apply it via Terraform: supply readable configuration and variable values, then use init, plan, and apply to deploy changes.
How it connects
Connects input variables to HCL variable files and JSON as an alternative configuration format.

Also explained in

Compare how 3 books explain HashiCorp Configuration Language →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.