Introduction to Terraform (2026 Guide)
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It lets you define and provision infrastructure across virtually any cloud or service using a declarative configuration language. This step-by-step guide takes you from the absolute basics to advanced techniques, fully updated for 2026 — including the latest workflow, modules, state management, and the rise of OpenTofu.

Terraform vs. OpenTofu in 2026
Since HashiCorp moved Terraform to the BSL license, the community fork OpenTofu (a Linux Foundation project) has become a popular fully open-source, drop-in alternative. The HCL syntax and workflow are nearly identical, so everything in this guide applies to both. Choose Terraform for the HashiCorp ecosystem and HCP integration, or OpenTofu if you need a permissively licensed tool.
Why Use Terraform?
- Declarative — describe the desired end state; Terraform figures out how to get there.
- Multi-cloud — one tool and workflow across AWS, Azure, GCP, Kubernetes, and 1,000+ providers.
- Versioned & reviewable — infrastructure changes go through Git and code review.
- Repeatable — spin up identical environments (dev/staging/prod) on demand.
Installing Terraform
# macOS (Homebrew)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# Ubuntu / Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
# Verify
terraform -versionThe Core Terraform Workflow
Almost everything in Terraform follows the same four-command loop:
terraform init # download providers & set up the backend
terraform plan # preview the changes Terraform will make
terraform apply # create/update/destroy resources to match config
terraform destroy # tear everything downYour First Configuration (AWS Example)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c7217cdde317cfec"
instance_type = "t3.micro"
tags = {
Name = "terraform-web-2026"
}
}Run terraform init, then terraform plan, then terraform apply. Terraform provisions the EC2 instance and records it in state.
Variables, Outputs & Data Sources
# variables.tf
variable "instance_type" {
type = string
default = "t3.micro"
}
# outputs.tf
output "public_ip" {
value = aws_instance.web.public_ip
}
# data source — look up the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}State Management (The Most Important Topic)
Terraform tracks real-world resources in a state file. Never store it locally for team projects — use a remote backend with locking so teammates don’t clobber each other’s changes.
terraform {
backend "s3" {
bucket = "my-tf-state-2026"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "tf-locks"
encrypt = true
}
}Tip: In 2026, S3 native state locking is available, and HCP Terraform / Terraform Cloud remains the easiest managed backend with remote runs, policy enforcement, and a private module registry.
Modules: Reusable Infrastructure
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
name = "prod-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
}Modules let you package and reuse infrastructure patterns. Use the public Terraform Registry for battle-tested modules, or build your own for internal standards.
Advanced Techniques for 2026
- Workspaces — manage multiple environments from one config.
- for_each & count — create resources dynamically. See our guide to the for_each meta-argument.
- Dynamic blocks — generate nested config programmatically.
- Policy as Code — enforce guardrails with Sentinel or Open Policy Agent (OPA).
- Terraform in CI/CD — run plan/apply via GitHub Actions with PR-based approvals (GitOps for infrastructure).
- Drift detection — schedule regular
terraform planruns to catch out-of-band changes.
Best Practices
- Keep state remote, encrypted, and locked.
- Pin provider and module versions.
- Never hardcode secrets — use variables, Vault, or cloud secret managers.
- Run
terraform fmtandterraform validatein CI. - Use small, composable modules and a clear directory structure.
Conclusion
Terraform (and OpenTofu) remains the backbone of modern infrastructure automation in 2026. Master the core workflow, treat state with care, modularize your code, and integrate it into CI/CD — and you’ll be able to manage infrastructure of any scale safely and repeatably. Ready to keep going? Compare it with config management in Terraform vs. Ansible, and prep with our 50 Terraform Interview Questions.

