Skip to main content
New: the Agentic AI Academy, 5 courses on running AI agents in production.Start free →
Back to Blog

Terraform HCL Syntax: A Practical Guide

Master HashiCorp Configuration Language (HCL) — the syntax behind every Terraform configuration. Blocks, arguments, expressions, and functions.

Luca BertonFebruary 25, 20261 min read

What Is HCL?

HashiCorp Configuration Language (HCL) is the language Terraform uses for configuration files. It's designed to be human-readable while being machine-parseable.

Basic Structure

HCL files use blocks, arguments, and expressions:

hcl
# Block type "resource", labels "aws_instance" and "web"
resource "aws_instance" "web" {
  # Arguments
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  # Nested block
  tags = {
    Name = "WebServer"
  }
}
Related course

Learn this topic with guided video lessons

Go beyond reading — follow expert video guidance and try the examples in your own environment.

Browse Courses →

Variables

hcl
variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}

variable "allowed_ports" {
  type    = list(number)
  default = [80, 443]
}

variable "tags" {
  type = map(string)
  default = {
    Environment = "dev"
    Team        = "platform"
  }
}

Reference with var.instance_type.

Outputs

hcl
output "instance_ip" {
  description = "Public IP of the web server"
  value       = aws_instance.web.public_ip
}

Expressions

hcl
# Conditional
instance_type = var.environment == "prod" ? "t2.large" : "t2.micro"

# String interpolation
name = "server-${var.environment}-${count.index}"

# For expressions
upper_names = [for name in var.names : upper(name)]
Stay updated

Get weekly IT automation tips

Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.

Subscribe Free →

Built-in Functions

hcl
# String functions
name = lower("MyServer")           # "myserver"
id   = substr(var.long_id, 0, 8)   # first 8 chars

# Collection functions
first = element(var.list, 0)
merged = merge(var.tags, { Name = "web" })

# File functions
key = file("~/.ssh/id_rsa.pub")

Locals

hcl
locals {
  common_tags = {
    Project     = var.project_name
    Environment = var.environment
    ManagedBy   = "terraform"
  }
}

resource "aws_instance" "web" {
  tags = local.common_tags
}

Learn More

Master HCL from scratch in our Terraform for Beginners course — practical exercises with real AWS infrastructure.

---

Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.

Ready to learn by doing?

Stop reading tutorials — start building. Expert video courses with practical examples you can follow at your own pace.

Share this article
LB
Luca Berton

Docker Captain, IT automation expert, Red Hat Summit & KubeCon speaker. Building hands-on education for DevOps engineers at CopyPasteLearn.

Related Articles

Explore topics

Browse more articles on the topics covered here.