Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Learn HCL: the language of Terraform and Packer, and its tools

9 min read
Improve this page

HCL (HashiCorp Configuration Language) is the language in which Terraform configurations and Packer templates are written: blocks, attributes and expressions, readable by a human and parseable by a program. The difficulty is not the syntax, which comes down to a few rules: it is knowing what comes from the language and what comes from the tool. This course is for anyone who writes or reviews Terraform or Packer code and wants to read these files without mixing the two up. It teaches the common form, then what Terraform and Packer add to it, and finally the tools for formatting, editing, converting and validating.

What you will learn

  • Read and write an HCL file by separating the language from the tool.
  • Write expressions and templates that produce the expected result.
  • Format, edit and convert HCL files from a script.
  • Validate and lint a configuration, and know what they miss.

What is HCL?

HCL is a library written by HashiCorp and embedded by several tools to read their configuration files. Its specification fixes a form: a file is a body made of attributes (name = value) and blocks (a type, labels, a body), with value types, expressions and templates. It exists in two syntaxes: the native syntax of .tf and .pkr.hcl files, and a JSON syntax.

variable "region" {
type = string
default = "eu-west-3"
}
locals {
name = "app-${var.region}"
}

This fragment means the same thing in Terraform and in Packer. But variable and locals are not HCL words: they are blocks that each tool declares in its schema, just like the variables and functions an expression may use.

Where does HCL come from, and why one more language?

HCL was born at HashiCorp, whose hashicorp/hcl repository was opened in 2014; its README aims it first at DevOps tools and servers driven from the command line. Its answer to the question "why not JSON or YAML?" is in that README: JSON and YAML are serialization formats for data, whereas HCL is designed to build configuration languages. It sits between generic data formats and configurations written in a real programming language such as Ruby: readable and handwritten, with a declarative logic (expressions, conditions, loops) that JSON does not have.

Two choices follow. The native syntax draws on the configuration files of nginx and on the libucl library, with blocks and comments. And HCL defines an equivalent JSON syntax, because JSON remains the common language of programs: a human writes native, a program generates JSON, and the tool reads both.

The current version is HCL 2, adopted by Terraform 0.12 in May 2019. It brought the full expression language, with types, for expressions and templates, which this course teaches. Old Terraform 0.11 configurations, written for HCL 1, are recognized by their "${var.x}" everywhere, now flagged as unnecessary by TFLint.

Where do you run into HCL?

You meet it wherever a HashiCorp tool, or a tool from its ecosystem, reads its configuration. The language is the same; the schema, that is the allowed blocks, changes on every row of the table:

ToolFilesWhat they describe
Terraform (in French) and OpenTofu (in French).tf, .tfvarsthe infrastructure to create
Packer.pkr.hcl, .pkrvars.hclhow an image is built
Terragrunt (in French)terragrunt.hclthe orchestration of several Terraform configurations
Nomad (in French)*.nomad.hcla job to schedule
Vault.hcl policiesaccess rights to secrets
TFLint.tflint.hclthe linter configuration

For a DevOps engineer, Terraform and Packer account for most of the usage, which is why the course uses them to test each behavior. Being able to read the common language means you can open any of these files without confusing what comes from HCL with what comes from the tool.

The language or the tool: the boundary that changes everything

The HCL specification explicitly leaves the function table and the variables to the application. Two tools that read the same text can therefore accept it differently, and the course tests this rather than asserting it:

Tested findingTerraform 1.16.1Packer 1.16.1
tostring(1), tobool("true")acceptedCall to unknown function
vault("secret/x", "k")unknown functionexisting function
variable named versionrejected, reserved nameaccepted
value from the environmentTF_VAR_namePKR_VAR_name

Keeping this boundary in mind avoids the most expensive mistakes: an expression copied from a Terraform module into a Packer template, or a setting looked up in the language documentation when it belongs to the tool.

What does the course cover?

The course follows the pattern of the YAML and JSON courses: correct writing first, then tooling. The full plan, with your progress, is on the page My course (in French).

The first module, Write the language, covers the common form: the syntax (in French) of attributes and blocks, the types (in French) and their conversions, the expressions and templates (in French), and the JSON syntax (in French), which programs generate.

The second, What the tool adds, compares Terraform and Packer on variables and locals (in French) and on functions (in French), then on the block schema: the blocks each tool accepts, the files it reads and its rejection messages.

The third, Tooling, works on files: the formatters (in French), hcledit (in French) to edit by script, hcl2json (in French) to query with jq, and validation with TFLint (in French).

What depends on the behavior of Terraform itself (providers, state, modules, Terraform-specific expressions and functions) lives in the Terraform course (in French), which this course prepares you for without repeating it.


My experience

An HCL syntax error costs little: terraform validate stops it before it reaches any infrastructure. What costs a lot are the silences that this course's lab brought to light: a misspelled function under try, accepted by validate and by TFLint; a hcledit attribute set with no effect that exits with code 0; a template that produces empty lines without any tool reporting it. Every behavior described was replayed on the tools cited, at the versions given, so that these silences become visible before you meet them in production.

What this course stands for

  • Separate the language from the tool. Before looking for a setting, know whether it belongs to HCL or to Terraform.
  • Format without discussion. terraform fmt in CI, -check -recursive, and no more layout debates in review.
  • Edit by address, never by pattern. hcledit rather than sed, and a re-read of the value after writing.
  • Generate JSON when a program writes the file. The language's JSON library guarantees the syntax.
  • Test the result, not just the form. validate and TFLint do not see a wrong value.

What I forbid

  • A try that wraps an entire function call: a typo becomes invisible inside it.
  • tflint --fix committed without review: it deletes the variables it considers unused.
  • A handwritten .tf.json: a reference outside ${...} becomes plain text with no error.
  • An expression copied from Terraform to Packer without testing it in the Packer console.

Tools and reference versions

The course was tested on 7 October 2026 with the following versions, aligned with the Terraform course, and every output comes from those runs:

ToolVersionRole in the course
Terraform1.16.1console, fmt, validate, JSON syntax
Packer1.16.1console, fmt, validate
TFLint0.64.0linter, built-in terraform ruleset
hcledit0.2.18editing by address
hcl2json0.6.9conversion to JSON
jq1.8.2querying the converted JSON

The block schema lesson, added afterwards, was tested on 19 November 2026 with Terraform 1.17.2 and Packer 1.16.2.

Key takeaways

  • HCL fixes the form: attributes, blocks, types, expressions, two syntaxes (native and JSON).
  • The allowed blocks, the variables and the functions come from the tool: Terraform and Packer differ.
  • fmt formats, hcledit edits, hcl2json converts, validate and TFLint check: each tool has a single role.
  • None of these tools checks the result: a test does.