HashiCorp Configuration Language (HCL) is a declarative configuration language designed to describe infrastructure and tool settings in a form that is readable by people and precise enough for machines. In Infrastructure as code for the Cloud, it is best known as the language of Terraform and OpenTofu, where it defines resources, providers, variables and modules.
HCL sits between a pure data format such as JSON and a general-purpose programming language. It offers blocks, attributes and expressions, including references, functions and conditional logic, while deliberately avoiding arbitrary control flow so that configurations remain predictable and analyzable before anything is changed.
How HCL structures a configuration
An HCL file is built from two primitives: attributes, which assign a value to a name, and blocks, which group attributes under a type and optional labels. A Terraform resource block such as resource "aws_instance" "web" has the block type resource and two labels that name the resource type and its local identifier.
The language specification defines a native syntax, normally stored in files ending in .tf or .hcl, and an equivalent JSON syntax for machine-generated configuration. Both map to the same information model, so tools can accept hand-written and generated files side by side.
The table below lists the main structural elements and their roles.
| Element | Example | Purpose |
|---|---|---|
| Attribute | instance_type = "t3.micro" | Assigns one value to a name |
| Block | resource "aws_s3_bucket" "logs" { } | Groups settings under a type and labels |
| Expression | var.region | Computes a value from references |
| Function call | cidrsubnet(var.cidr, 8, 1) | Transforms values with built-in logic |
| Comment | # primary network | Documents intent without effect |
Expressions, types and dependencies
HCL expressions give configurations controlled dynamism. Values can reference input variables, local values, data sources and attributes of other resources, and they can be combined with operators, conditional expressions of the form condition ? a : b, and for expressions that transform lists and maps.
The language has a type system with primitive types (string, number, bool) and structural types (list, set, map, object, tuple). Terraform uses type constraints on variables to reject invalid input early, and it performs automatic conversion where it is safe, such as turning the string “5” into a number.
References between blocks also create an implicit dependency graph. When a subnet refers to aws_vpc.main.id, the tool knows that the network must exist first. This lets Terraform and OpenTofu order and parallelize operations without explicit sequencing, while depends_on covers rare hidden dependencies.
The following table compares the most used expression features.
| Feature | Syntax pattern | Typical use |
|---|---|---|
| Reference | resource_type.name.attribute | Pass outputs between resources |
| Conditional | var.prod ? 3 : 1 | Vary size by environment |
| For expression | [for s in var.subnets : s.id] | Reshape collections |
| Splat | aws_instance.web[*].id | Collect one attribute from many |
| Dynamic block | dynamic "ingress" { } | Generate repeated nested blocks |
| Meta-argument | count or for_each | Create multiple instances |
HCL compared with other configuration formats
HCL is one of several ways to express infrastructure as code, and its position is defined by readability with limited logic. YAML and JSON carry data only, while tools such as Pulumi and AWS CDK use full programming languages. Bicep and HCL occupy the same middle ground of purpose-built declarative languages.
The comparison below shows how HCL relates to its main alternatives.
| Approach | Logic support | Readability | Main ecosystems |
|---|---|---|---|
| HCL | Expressions, loops, functions | High | Terraform, OpenTofu, Packer, Nomad |
| JSON | None | Low for large files | AWS CloudFormation, generated configs |
| YAML | None without templating | High until deeply nested | Ansible, Kubernetes, CloudFormation |
| Bicep | Expressions and loops | High | Microsoft Azure only |
| General-purpose languages | Unlimited | Depends on code quality | Pulumi, AWS CDK |
The main advantage of HCL over general-purpose languages is that a configuration can be evaluated without executing arbitrary code, which simplifies review, static analysis and policy as code checks. The trade-off is that complex abstractions can feel verbose or awkward compared with a real programming language.
Where HCL is used and how to write it well
Beyond Terraform and OpenTofu, HCL appears in Packer templates, Nomad job specifications, Consul and Vault configuration, and in Terragrunt files. Each tool defines its own schema of permitted blocks on top of the shared language, so the syntax is common while the vocabulary differs.
Well-maintained HCL code follows a few consistent conventions: one purpose per file, such as variables.tf, outputs.tf and main.tf; descriptive resource names; typed variables with descriptions and validation rules; and automatic formatting with terraform fmt or tofu fmt. Repeated patterns belong in a reusable Terraform module rather than in copied blocks.
The decision table below covers frequent design choices.
| Situation | Recommended construct | Reason |
|---|---|---|
| Instances keyed by name | for_each with a map | Stable addresses when items change |
| Identical copies by number | count | Simple scaling of uniform resources |
| Repeated derived values | locals block | One definition, many references |
| Environment differences | Variables and tfvars files | Same code, different inputs |
| Invalid input risk | Variable validation blocks | Fails before planning starts |
Limitations and common mistakes
HCL is intentionally not Turing-complete in everyday use, so complex logic often ends up in long expressions that are difficult to read and test. Error messages for type mismatches inside nested objects can also be hard to interpret, especially for newcomers.
- Using
countfor lists whose order changes, which causes unexpected destroy and recreate operations. - Hardcoding regions, account identifiers or secrets instead of passing them as typed input variables.
- Writing secrets in plain text, forgetting that values can still appear in Terraform state even when marked sensitive.
- Overusing dynamic blocks, which makes configurations harder to review than explicit blocks.
- Confusing HCL syntax with tool behavior: the language defines structure, while each tool decides meaning.
A non-technical view
HashiCorp Configuration Language is a structured way of writing down what infrastructure should exist. Think of it as a detailed order form. You list the servers, networks and storage you need. A tool such as Terraform then builds exactly that.
The form allows a little intelligence, such as “three servers in production, one in testing”. It still stays simple enough for colleagues to read and check. The key point is that the file describes the result, not the steps, and the tool works out how to get there.
A concrete example
A platform team at a mid-sized online retailer manages three environments, development, staging and production, on a public cloud. Previously, engineers created resources by hand in the web console, and the environments slowly drifted apart.
The team rewrites the setup in HCL as one Terraform module for networking, one for a container cluster and one for databases. A variable named environment controls instance sizes through a conditional expression, and `for_each` creates subnets in three availability zones from a single map. Every change now goes through a pull request where reviewers read the HCL diff and the plan output.
Within one quarter, staging becomes a faithful copy of production at smaller scale, a new region is added by supplying a different variables file, and configuration drift is detected by scheduled plan runs. The readable, reviewable files turned infrastructure changes from manual console work into a routine, auditable process.