Introduction to Terraform
Learn what Terraform is, how Infrastructure as Code works, why it beats clicking through cloud consoles, and how Terraform compares to Ansible, CloudFormation and Pulumi.
What Is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool created by HashiCorp. It lets you describe your entire cloud environment, servers, networks, databases, DNS records, load balancers, in plain text configuration files, and then creates, updates, or destroys that infrastructure automatically.
Instead of clicking through the AWS Console to launch an EC2 instance, you write a few lines of configuration like this:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
}
Run one command (terraform apply) and the server exists. Run another (terraform destroy) and it is gone. No clicks, no forgotten settings, no "I wonder what I configured last time."
What Is Infrastructure as Code?
Infrastructure as Code (IaC) means treating your infrastructure the same way developers treat application code: write it in files, store it in Git, review it in pull requests, and deploy it automatically.
The problem IaC solves
Before IaC, teams managed infrastructure manually. A senior engineer would click through the cloud console, configure something, and the knowledge of how it was configured lived only in their head, or a messy wiki page nobody kept up to date.
This caused predictable problems:
- "Works on staging but not production": because the two environments were configured slightly differently by different people at different times.
- Disaster recovery failures: because nobody could recreate the infrastructure from scratch without spending days doing it by hand.
- No change history: there was no way to see what changed, who changed it, or why.
- Slow deployments: spinning up a new environment for a new customer could take days of manual work.
What IaC gives you
| Benefit | What it means in practice |
|---|---|
| Repeatability | Spin up identical dev, staging and prod environments from the same code |
| Version control | Every infrastructure change is a Git commit with an author, timestamp and message |
| Code review | A teammate reviews your terraform plan output before anything changes in production |
| Disaster recovery | If a region goes down, re-create the entire environment in another region with one command |
| Documentation | The configuration files are the documentation, always accurate, never out of date |
| Speed | Build a 20-resource environment in 2 minutes instead of 2 days |
How Terraform Works: The Big Picture
Terraform follows a simple mental model:
- You write: describe the infrastructure you want in
.tffiles using HCL - Terraform plans: compares your desired state to what currently exists and shows you exactly what will change
- Terraform applies: makes the API calls to create, update or delete resources
- Terraform records: writes the result to a state file so it knows what it manages
Terraform Core Workflow
The state file is what prevents Terraform from creating duplicate resources
The state file is critical. It is what allows Terraform to say "this resource already exists, I just need to update its tags" rather than creating a duplicate.
Core Concepts You Will Use Every Day
Providers
A provider is a plugin that lets Terraform communicate with a specific platform. When you declare:
provider "aws" {
region = "us-east-1"
}
Terraform downloads the AWS provider, which contains code for every AWS resource type (aws_instance, aws_s3_bucket, aws_vpc, etc.).
There are providers for:
- Cloud platforms: AWS, Azure, Google Cloud
- Kubernetes and Helm
- Databases: PostgreSQL, MySQL
- SaaS tools: GitHub, PagerDuty, Datadog, Cloudflare
This is what makes Terraform genuinely cloud-agnostic, you learn one workflow and apply it to any platform.
Resources
A resource is anything Terraform manages, a server, a bucket, a DNS record, a Kubernetes deployment. Every resource block follows the same pattern:
resource "<provider>_<type>" "<local_name>" {
argument = "value"
}
For example:
resource "aws_s3_bucket" "my_bucket" {
bucket = "devopslesson-demo-2026"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
The local name (my_bucket, main) is only used inside your Terraform code to reference the resource, it is not the name of the resource in AWS.
Data Sources
Sometimes you need to look up existing infrastructure rather than create new things. That is what data sources are for:
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*"]
}
}
This finds the latest Ubuntu 22.04 AMI without hardcoding an AMI ID that will eventually expire.
Variables and Outputs
Variables make your configuration flexible and reusable. Outputs expose values, like an IP address or a database endpoint, after apply:
variable "instance_type" {
description = "EC2 instance type"
default = "t3.micro"
}
output "server_ip" {
value = aws_instance.web.public_ip
}
These are covered in full in the Variables and Outputs tutorial.
Terraform vs Other IaC Tools
It is useful to know where Terraform sits relative to the other common tools.
Terraform vs Ansible
This is the most common comparison. The key difference is what each tool manages:
- Terraform provisions infrastructure, it creates the VM, the network, the database. It talks to cloud provider APIs.
- Ansible configures what runs on that infrastructure, installing Nginx, editing config files, creating users.
Many teams use both: Terraform builds the servers, Ansible configures them. They are complementary, not competing.
Terraform vs AWS CloudFormation
CloudFormation is Amazon's native IaC tool. The key differences:
| Feature | Terraform | CloudFormation |
|---|---|---|
| Cloud support | Any provider (AWS, Azure, GCP, …) | AWS only |
| Language | HCL (readable, concise) | JSON or YAML (verbose) |
| State management | You control the state file | AWS manages state |
| Error messages | Generally clear | Notoriously confusing |
| Ecosystem | Massive (Terraform Registry) | AWS-only modules |
If you only ever use AWS and your team is deeply embedded in the AWS ecosystem, CloudFormation is a reasonable choice. For everything else, Terraform wins on flexibility.
Terraform vs Pulumi
Pulumi is the most modern alternative. It lets you write infrastructure in TypeScript, Python or Go instead of HCL. The tradeoffs:
- Pulumi: More expressive (full programming languages), steeper learning curve, smaller ecosystem
- Terraform: Simpler syntax, massive community, the de-facto industry standard, more job postings
For most engineers, Terraform is the right starting point. Learn Terraform first; you can always pick up Pulumi later.
The Terraform Workflow in One Minute
You will go deep on each command in the next tutorials, but here is the overview:
# 1. Download providers and initialise the working directory
terraform init
# 2. Preview what will change, no infrastructure is touched
terraform plan
# 3. Create or update infrastructure
terraform apply
# 4. Tear everything down when you're done
terraform destroy
The plan step is Terraform's superpower. You see exactly what will be created, changed or deleted, with counts and attribute diffs, before you commit. This makes infrastructure changes as reviewable as code changes.
What This Series Covers
This tutorial series teaches Terraform from zero to production-ready, in order:
- Introduction (this page), what Terraform is and why it matters
- Installing Terraform: install on macOS, Linux or Windows and run your first config
- The Core Workflow:
init,plan,apply,destroyin depth - Variables and Outputs: make your code flexible and reusable
- State and Backends: understand the state file and use remote backends
- Modules: package infrastructure into reusable components
Each tutorial builds on the last. By the end you will have the skills to provision real cloud infrastructure safely and confidently.
Knowledge Check
Question 1: What is Terraform?
Which of the following best describes what Terraform does?
Question 2: The State File
Why does Terraform maintain a state file?
Question 3: Terraform vs Ansible
Your team needs to: (1) create an EC2 instance on AWS, and (2) install Nginx on it. Which tool handles each task?
Question 4: Declarative vs Procedural
Terraform is described as "declarative". What does this mean?