interview-prep

Crisp answer: HCL (HashiCorp Configuration Language) is Terraform's configuration language. It uses blocks, arguments, and expressions. It is not a general-purpose programming language — it is a configuration format with limited logic.

Basic syntax:

# This is a comment

# Block: type label label { body }
resource "aws_instance" "web" {
  ami           = "ami-0abc123"    # string
  instance_type = "t3.micro"
  count         = 2               # number
  monitoring    = true            # bool

  tags = {                        # map(string)
    Name        = "web-server"
    Environment = "production"
  }

  root_block_device {             # nested block
    volume_size = 20
    volume_type = "gp3"
  }
}

Types:

Type Example
string "eu-west-2"
number 3, 0.5
bool true, false
list(string) ["a", "b", "c"]
map(string) { key = "value" }
object({...}) Structured object with named attributes
any Accept any type

Expressions:

# String interpolation
name = "web-${var.environment}"

# References
subnet_id = aws_subnet.private.id
ami       = data.aws_ami.ubuntu.id

# Conditional expression (ternary)
instance_type = var.environment == "prod" ? "m5.large" : "t3.small"

# Functions
name    = lower("MY-APP")              # "my-app"
cidr    = cidrsubnet("10.0.0.0/16", 8, 1)  # "10.0.1.0/24"
length  = length(var.availability_zones)
joined  = join(",", ["a", "b", "c"])   # "a,b,c"
encoded = base64encode("hello")

for expressions:

# List comprehension
names = [for s in var.services : upper(s)]
# var.services = ["web", "api"] → ["WEB", "API"]

# Map comprehension
tags = { for k, v in var.raw_tags : k => lower(v) }

# Filtering
prod_subnets = [for s in aws_subnet.all : s.id if s.tags["Env"] == "prod"]

Heredoc strings:

user_data = <<-EOF
  #!/bin/bash
  apt-get update
  apt-get install -y nginx
  echo "Hello from ${var.environment}" > /var/www/html/index.html
EOF

What to say in the interview:

"HCL is a declarative configuration language, not a general-purpose language. It has types, expressions, string interpolation, and limited logic via conditionals and for expressions. The key mental model: everything is either a block (resource, variable, output, module, data) or an argument inside a block. Expressions reference other resources and data sources to wire things together."


My notes