Terraform Tutorial — HCL Syntax
HCL (HashiCorp Configuration Language) is the language you write Terraform configurations in. This chapter teaches you HCL from the ground up — blocks, arguments, types, string interpolation, collections, and for expressions.
What’s in this Chapter?
| # | Topic | Description |
|---|---|---|
| 1 | Blocks, Arguments & Expressions | Block types, key-value pairs, references |
| 2 | String Interpolation & Types | Strings, numbers, bools, heredocs, templates |
| 3 | Lists, Maps & Objects | Collection types, accessing elements, functions |
| 4 | For Expressions | Transforming lists and maps, filtering |
| 5 | terraform fmt & validate | Auto-formatting and syntax checking |
| 6 | Practice Exercises | Hands-on HCL writing exercises |
Chapter Overview
HCL is designed to be human-readable and machine-parseable. It sits between JSON (too verbose, no comments) and YAML (ambiguous, whitespace-sensitive). HCL gives you a clean syntax with explicit block structure, built-in type system, and powerful expressions.
You don’t need to memorize every detail. This chapter is a reference you’ll return to as you write more Terraform. Focus on understanding blocks and arguments first — those are the foundation of every .tf file.
Key Concepts Preview
| Term | Definition |
|---|---|
| Block | A container for configuration — resource {}, variable {}, output {} |
| Argument | A key-value pair inside a block — region = "us-east-1" |
| Expression | A value that can include references, operators, or function calls |
| Interpolation | Embedding a variable or expression inside a string — "Hello, ${var.name}" |
| Type | The data type of a value — string, number, bool, list, map |
What You’ll Be Able to Do After This Chapter
- Read and write HCL block structures fluently
- Use string interpolation and heredocs for dynamic values
- Work with lists, maps, and complex object types
- Transform collections using for expressions
- Keep your code clean with
terraform fmtand catch errors withterraform validate