What Is HCL?
Open any Terraform project and you will see files ending in .tf. They are not JSON, not YAML and not a programming language, though they borrow a little from each. They are written in HCL, and you will read it every day.
The rule
HCL is blocks of settings; every .tf file in a folder is one configuration.
HCL (HashiCorp Configuration Language) was designed to be easy for people to read and write while still being precise enough for a tool. It is declarative: you describe things, you do not write step-by-step instructions.
Here is the start of Crumb's configuration:
# Crumb bakery: storage for cake photos
resource "aws_s3_bucket" "assets" {
bucket = "crumb-bakery-assets"
tags = {
team = "web"
}
}Things to notice:
- Blocks wrap related settings in
{ }. - Arguments are
name = valuelines. - Comments start with
#(also//, and/* ... */for several lines). - Values can be strings, numbers,
true/false, lists[...]and maps{...}.
Terraform reads every .tf file in the current folder (not sub-folders) and treats them as one configuration. The order of blocks and files does not matter. The usual split is purely for people:
main.tf— resourcesvariables.tf— inputsoutputs.tf— resultsproviders.tforversions.tf— provider settings
There is also a JSON form, .tf.json, meant for files that programs generate. People write .tf.
Whitespace is flexible: indentation and blank lines are for readers, and terraform fmt rewrites files into the standard layout, so teams never need to argue about spacing.
The shortcut
When you open an unfamiliar project, read it as nouns, not steps. Each block is a thing that should exist; the file it lives in and its position are irrelevant.
List the files
main.tf,variables.tf,outputs.tf. Terraform merges them into one configuration.
The trap: putting a .tf file in a sub-folder and expecting it to load. Sub-folders are separate modules, not part of this configuration.
Drills
Answer fast — trust the rule.
Which files does Terraform load from the working folder?
What is HCL's style?
You move a resource block from the top of main.tf into a new file storage.tf in the same folder. What changes in the plan?
Which line is a valid HCL comment?
Remember
- HCL is declarative: blocks of
name = valuesettings. - Shortcut: read blocks as nouns; file names and order do not matter.
- Trap:
.tffiles in sub-folders are not loaded as part of this configuration.
