HCL Syntax Fundamentals
What you'll learn: How HashiCorp Configuration Language (HCL) structures your infrastructure code using blocks, arguments, and expressions.
The Building Blocks of HCL
HashiCorp Configuration Language is Terraform's native language for defining infrastructure. Think of it like writing a recipe: you need ingredients (arguments), cooking steps (blocks), and sometimes you need to calculate quantities (expressions).
Blocks: The Containers
Blocks are the primary structure in HCL. They define resources, variables, or configuration sections. A block has a type, optional labels, and a body wrapped in curly braces:
resource "aws_instance" "web_server" {
# block body goes here
}
The type is resource, the labels are "aws_instance" and "web_server", and everything inside {} is the body.
Arguments: The Key-Value Pairs
Inside blocks, arguments assign values to names. They follow a simple name = value pattern:
ami = "ami-12345678"
instance_type = "t2.micro"
Expressions: The Dynamic Parts
Expressions compute or reference values. They can be simple literals ("hello"), references to other resources (aws_instance.web_server.id), or calculations:
count = var.instance_count * 2
Putting It Together
HCL files typically contain multiple blocks with nested arguments and expressions working together to describe your desired infrastructure state in a human-readable format.
Key Takeaway: HCL uses blocks to organize configuration sections, arguments to set properties, and expressions to create dynamic, reusable values—forming the foundation of all Terraform code.