HCL language
A syntax and API for building structured configuration languages: bodies of blocks and attributes plus an expression and template sub-language, with the application supplying the schema and functions.
Overview
HCL is the HashiCorp configuration language: a notation for structured configuration plus the toolkit that parses and evaluates it. It exists because JSON and YAML serialize data without letting an application define a schema, while a general-purpose language turns a configuration file into a program.
Code
// An attribute binds a name to an expression; literals are the atoms.
port = 8080 // number
enabled = true // bool
region = "eu-west-1" // string, itself a template
ratio = 0.75
sum = 1 + 2 * 3 // * binds tighter than +
flag = !enabled || port > 1024
// Tuples and objects; the operators that consume them.
zones = ["a", "b", "c"]
tags = { team = "infra", tier = "web" } // a literal key is a name
first = zones[0] // index
team = tags.team // attribute access
sizes = [for z in zones : upper(z)] // tuple for-expression
lookup = { for z in zones : z => length(z) } // object for-expression
// A block: a type, optional labels, and a child body the application validates.
service "http" "web_proxy" {
listen_addr = "127.0.0.1:8080"
process "main" { // a nested block
command = ["app", "server"]
}
}
shout = upper("done") // functions come from the host application
// Templates: interpolation, directives, heredocs, and the splat shorthand.
name = "app-${zones[0]}" // interpolation of an expression
list = "%{ for n in zones }${n},%{ endfor }" // a template directive
doc = <<-EOT
first zone is ${zones[0]}
%{ if length(zones) > 1 } multi-zone%{ endif }
EOT
ids = nodes.*.id // attribute-only splat
for expressions
and splats,
and the block gives the application a child body to validate. Note what is
absent: no assignment, no loop statement, no way to name a block as a value —
those belong to the information model,
which only the calling application can interpret.Distinctive traits
- Bodies, not schemas. The application declares the attribute names and block types it accepts, so one notation serves unrelated tools (schema-driven processing).
- Three co-designed sub-languages. A structural language for blocks and attributes, an expression sub-language for values, and a template language for strings.
- Unknown values are first-class. A value may be unknown of a known type, which is what lets a caller plan from partial information instead of failing (unification).
- Two syntaxes, one model. The native syntax and the JSON profile decode to the same bodies, so generated and hand-written configuration mix.
- Coercion is specified. Conversions between primitive types are marked safe or unsafe, and unsafe ones may fail rather than silently lose information.
- Static analysis is a separate reading. Lists, maps, calls and traversals can be inspected without evaluating them, which is how tools lint a configuration they cannot fully resolve.
Status
Active: HCL 2.x is a Go module under the MPL 2.0, with the native syntax specification and the information model published alongside the code. Terraform and OpenTofu are its largest consumers; other HashiCorp tools read the same notation.
History
HCL and its predecessor HIL — the interpolation language — were written for Terraform in 2014, and the repository's earliest commit is dated August 2014. HCL 2.0 merged the two into a single language with arbitrary expressions and a new parser and Go API, released in January 2020. The native syntax is credited in the README as inspired by libucl and nginx configuration.