Home

What is Terraform?

HashiCorp Terraform is an open-source Infrastructure-as-Code (IaC) tool that lets you provision and manage the infrastructure your applications need — VMs, networks, databases, IAM, anything an API can reach — programmatically. (IaC on Wikipedia.)

Infrastructure as Code is the practice of describing your infrastructure in version-controlled files and applying those files to bring the real world into that desired state, instead of clicking around a console or sshing into a box and editing things by hand. Beyond the obvious "you can grep your infra," it gives you reviewable diffs, rollbacks, and reproducibility across environments.

Terraform users describe infrastructure in HCL (HashiCorp Configuration Language) — a JSON-ish DSL designed to be easy to read and write. The same .tf files can target dozens of providers (AWS, GCP, Azure, Cloudflare, Kubernetes, GitHub, …) so you can manage a whole platform from one place.

A small example — a VM with an extra disk on GCP:

hcl
resource "google_compute_disk" "additional_disk" {  name                      = "vm-additional-disk"  type                      = var.additional_disk_type  size                      = var.additional_disk_size  zone                      = var.instance_zone  physical_block_size_bytes = 4096}resource "google_compute_instance" "gce" {  name         = "virtualmachine"  machine_type = var.virtualmachine_instance_type  zone         = var.virtualmachine_zone  tags         = var.virtualmachine_network_tag  boot_disk {    initialize_params {      image = var.boot_disk_image      size  = var.boot_disk_size    }  }  attached_disk {    source      = google_compute_disk.additional_disk.self_link    device_name = google_compute_disk.additional_disk.name  }  network_interface {    subnetwork = var.subnet_name    access_config {}  }}

How Terraform works

You describe the desired state in .tf files. When you run terraform plan, Terraform parses your config, compares it to the current state (stored in a state file), and shows you a diff of what it's about to do. terraform apply executes that diff by translating each change into API calls against the relevant provider. Because it's open source, you can always extend it with custom providers or pin to specific versions.

A typical day-to-day loop:

bash
terraform init     # download providers and set up the working directoryterraform fmt      # auto-formatterraform validate # syntax checkterraform plan     # see what will changeterraform apply    # actually change it

Installing Terraform

Ubuntu

Install the dependencies:

bash
sudo apt updatesudo apt install software-properties-common gnupg2 curl

Add and trust the HashiCorp apt repo:

bash
curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpgsudo install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Install:

bash
sudo apt updatesudo apt install terraform

Verify:

bash
terraform -v
bash
Terraform v1.2.2on linux_amd64

macOS

bash
brew tap hashicorp/tapbrew install hashicorp/tap/terraform

For other operating systems and the latest install instructions, see HashiCorp's install guide.

Quick note on OpenTofu: in 2023 a fork of Terraform called OpenTofu was created (via the Linux Foundation) after HashiCorp moved Terraform from MPL 2.0 to BUSL. The HCL is the same and most modules port cleanly. Worth keeping on your radar if licensing matters for your team.