Terraform Tutorial · Terraform Basics · Lesson 3 of 25
Terraform Syntax (HCL)
Blocks, arguments, labels, comments and expressions: how Terraform configuration files are written.
Blocks and arguments
Terraform files are made of blocks. A block has a type, zero or more labels, and a body in braces with
arguments (name = value) and nested blocks.
resource "aws_instance" "web" { # block type "resource", labels "aws_instance" and "web"
ami = "ami-0abcdef1234567890"
instance_type = "t3.micro"
tags = { # an argument whose value is a map
Name = "web-server"
}
}
Comments
# and // start a single-line comment; /* … */ spans several lines. # is the usual style.
Values
output "examples" {
value = {
text = "a string with ${upper("interpolation")}"
number = 42
decimal = 3.5
enabled = true
list = ["a", "b", "c"]
map = { env = "dev", team = "web" }
}
}
Output
examples = {
"decimal" = 3.5
"enabled" = true
"list" = [
"a",
"b",
"c",
]
"map" = {
"env" = "dev"
"team" = "web"
}
"number" = 42
"text" = "a string with INTERPOLATION"
}Run this on your computer — see “Get Started” for setup.
Files and order
Terraform reads every .tf file in the directory together, so the order of blocks and files doesn't matter.
By convention: main.tf (resources), variables.tf, outputs.tf, providers.tf and versions.tf.
Run terraform fmt to align = signs and indentation automatically.
Test yourself
Does the order of blocks in .tf files matter?
YesNo
Show answer
Answer: No
Terraform loads all files in the directory and builds a dependency graph.