HCL Syntax Reference
Attributes
Section titled “Attributes”Simple key-value pairs:
name = "my-app"count = 42enabled = trueKeys can be identifiers or quoted strings:
simple_key = "value""key-with-dashes" = "value""key.with.dots" = "value"Blocks
Section titled “Blocks”Blocks group related configuration:
# Block with no labelsterraform { required_version = ">= 1.0"}
# Block with one labelprovider "aws" { region = "us-west-2"}
# Block with two labelsresource "aws_instance" "web" { ami = "ami-12345"}Nested Blocks
Section titled “Nested Blocks”resource "aws_instance" "web" { ami = "ami-12345"
ebs_block_device { device_name = "/dev/sda1" volume_size = 100 }
tags { Name = "WebServer" }}Data Types
Section titled “Data Types”Strings
Section titled “Strings”# Simple stringsname = "hello world"
# Escape sequencesescaped = "line1\nline2\ttabbed"quoted = "say \"hello\""backslash = "path\\to\\file"
# Heredocs for multi-line stringsdescription = <<EOFThis is amulti-line stringEOF
# Indented heredocs (strips leading whitespace)script = <<-EOF #!/bin/bash echo "Hello" EOFNumbers
Section titled “Numbers”integer = 42negative = -17float = 3.14159scientific = 1.5e10hex = 0xFFoctal = 0o755Booleans
Section titled “Booleans”enabled = truedisabled = falseoptional_value = nullArrays (Tuples)
Section titled “Arrays (Tuples)”# Simple arrayports = [80, 443, 8080]
# Mixed typesmixed = ["string", 42, true, null]
# Multi-linetags = [ "web", "production", "critical",]
# Nested arraysmatrix = [ [1, 2, 3], [4, 5, 6],]Objects (Maps)
Section titled “Objects (Maps)”# Inline objectmetadata = { name = "app", version = "1.0" }
# Multi-line objectconfig = { timeout = 30 retries = 3 enabled = true}
# Nested objectssettings = { database = { host = "localhost" port = 5432 } cache = { host = "redis" port = 6379 }}Comments
Section titled “Comments”# Hash comment (preferred style)name = "value"
// Double-slash commentcount = 42
/* Block comment can span multiple lines */enabled = true
/* * Formatted block comment */port = 8080String Interpolation
Section titled “String Interpolation”name = "world"greeting = "Hello, ${name}!" # "Hello, world!"
# With expressionscount = 5message = "Found ${count} items"
# Nested accessserver = { host = "localhost" port = 8080}url = "http://${server.host}:${server.port}"Operators
Section titled “Operators”Arithmetic
Section titled “Arithmetic”sum = 5 + 3 # 8diff = 10 - 4 # 6product = 6 * 7 # 42quotient = 20 / 4 # 5remainder = 17 % 5 # 2negation = -valueComparison
Section titled “Comparison”equal = a == bnot_equal = a != bless = a < bgreater = a > bless_eq = a <= bgreater_eq = a >= bLogical
Section titled “Logical”and_result = a && bor_result = a || bnot_result = !aConditional (Ternary)
Section titled “Conditional (Ternary)”result = condition ? "yes" : "no"port = production ? 443 : 8080Collection Access
Section titled “Collection Access”Index Access
Section titled “Index Access”ports = [80, 443, 8080]first = ports[0] # 80second = ports[1] # 443Attribute Access
Section titled “Attribute Access”server = { host = "localhost" port = 8080}hostname = server.host # "localhost"Splat Expressions
Section titled “Splat Expressions”users = [ { name = "alice" }, { name = "bob" },]
# Get all namesnames = users[*].name # ["alice", "bob"]names = users.*.name # Alternative syntaxFor Expressions
Section titled “For Expressions”List Comprehension
Section titled “List Comprehension”numbers = [1, 2, 3, 4, 5]
# Transform each elementdoubled = [for n in numbers : n * 2]# [2, 4, 6, 8, 10]
# Filter with ifevens = [for n in numbers : n if n % 2 == 0]# [2, 4]Object Comprehension
Section titled “Object Comprehension”users = ["alice", "bob", "charlie"]
# Create object from listuser_map = { for name in users : name => upper(name) }# { "alice" = "ALICE", "bob" = "BOB", "charlie" = "CHARLIE" }Key-Value Iteration
Section titled “Key-Value Iteration”source = { a = 1 b = 2}
# Iterate with key and valueswapped = { for k, v in source : v => k }# { 1 = "a", 2 = "b" }Function Calls
Section titled “Function Calls”# Built-in style functionsupper_name = upper("hello")file_content = file("config.json")encoded = base64encode("data")
# Functions are preserved as structures when not evaluatedpassword = sensitive("secret123")# { "__function__": "sensitive", "__args__": ["secret123"] }