Thuta Learning
BasicDevOpsintermediate

HCL Syntax Basics

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand HCL Syntax Basics, no intimidation required
  • Get hands-on running terraform commands and HCL code yourself
  • Be ready to apply this concept in a real project right away

Let's think about this for a second

HCL syntax is designed to be more readable than JSON/YAML — you'll see the pattern `block_type "label" { argument = value }` over and over. Block types (resource, provider, variable, output) are Terraform's 'keywords.' An argument follows the `key = value` pattern — the value can be a string, number, boolean, list, or map. An expression is how you reference another resource's output using `${...}` syntax (which you can omit in modern HCL) to connect values together.

Let's connect this to a real scenario

Code like `resource "local_file" "greeting" { filename = "hello.txt"; content = "Hello, Terraform!" }` reads as: 'a resource of type local_file, named greeting in my config, with two arguments — filename and content.' You write comments with `#` (or `//`), and multi-line strings with heredoc syntax (`<<EOF ... EOF`).

Let's look at an example

hcl
# This is a comment
resource "local_file" "greeting" {
  filename = "hello.txt"
  content  = "Hello, Terraform!"
}

variable "environment" {
  type    = string
  default = "dev"
}

locals {
  tags = {
    project = "tutorial"
    owner   = "student"
  }
}
You should see
Be able to read HCL block/argument/expression syntax.

5-minute try-it

Read the HCL code above and write out, for each of the 3 block types (resource, variable, locals), what its label and arguments are.

A quick word of caution

HCL files use the `.tf` extension — Terraform loads every `.tf` file in a folder together as one combined configuration, not as separate individually-run files.

Easy traps

  • Assuming HCL is syntactically like YAML/JSON and is indentation-sensitive — HCL is actually a `{ }` brace-based syntax; indentation is just style, not a requirement
  • Forgetting to quote (`"..."`) a string value (this will cause an error)

Now try it yourself

Read the HCL code above and write out, for each of the 3 block types (resource, variable, locals), what its label and arguments are.

You'll know it worked when: Be able to read HCL block/argument/expression syntax.

HCL Syntax Basics | Thuta Learning