Skip to main content

HCL

HCL (HashiCorp Configuration Language) is the declarative language used to write Terraform files. It is readable by humans and parseable by machines, sitting between JSON and a full programming language.

What Is HCL?

Every .tf file is written in HCL, and tools like Packer, Vault, Consul, and Nomad use it too. It supports comments, string interpolation, conditionals, built-in functions, and for expressions, but you cannot define your own functions.

HCL
resource "aws_s3_bucket" "order_images" {
bucket = "stripe-${var.environment}-images" # interpolation
tags = {
Environment = var.environment == "prod" ? "production" : "non-prod" # ternary
}
}

Terraform also accepts .tf.json, which suits machine-generated files. Run terraform fmt to keep style consistent.

Remember

HCL is declarative. Use for_each, for expressions, and ternaries instead of loops and if statements.

Frequently Asked Questions

Why not use JSON or YAML for Terraform?

JSON has no comments or expressions, and YAML's whitespace rules make large files fragile. HCL supports both while staying readable.

Is HCL a programming language?

No. It is a declarative configuration language with expressions and built-in functions only.

Found a mistake, an outdated step or a broken link?