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:
# 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"
}
}Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Variables
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
output "instance_ip" {
description = "Public IP of the web server"
value = aws_instance.web.public_ip
}Expressions
# 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)]Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Built-in Functions
# 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
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 hands-on labs in real sandboxed environments.
Related Articles
Terraform CDK vs HCL Comparison
Terraform CDK lets you write infrastructure in TypeScript, Python, or Go instead of HCL. Compare CDKTF and HCL for real-world use cases and learn when each.
Terraform Variables and Outputs
Terraform variables and outputs. Types, validation rules, locals, sensitive values, tfvars files, and cross-module dependencies.
Tofu vs Terraform Comparison
OpenTofu forked Terraform after the BSL license change. Compare features, compatibility, licensing, and ecosystem to decide which IaC tool fits your team.
Terraform Import Existing Resources
Import existing infrastructure into Terraform. Import blocks, auto-generated config, state manipulation, and migration strategies.
Terraform Lifecycle Rules
Master Terraform lifecycle rules — prevent_destroy, create_before_destroy, ignore_changes, and replace_triggered_by for safe infrastructure updates.
Terraform Loops and Conditionals
Terraform loops and conditionals. count, for_each, for expressions, dynamic blocks, and advanced patterns for DRY config code.
Explore topics
Browse more articles on the topics covered here.