Introduction to Terraform

By Vishvesh Patel · DevOps EngineerUpdated July 12, 2026

What Terraform is and how Infrastructure as Code works. Why it beats clicking through consoles, and how it compares to Ansible and CloudFormation.

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

BenefitWhat it means in practice
RepeatabilitySpin up identical dev, staging and prod environments from the same code
Version controlEvery infrastructure change is a Git commit with an author, timestamp and message
Code reviewA teammate reviews your terraform plan output before anything changes in production
Disaster recoveryIf a region goes down, re-create the entire environment in another region with one command
DocumentationThe configuration files are the documentation, always accurate, never out of date
SpeedBuild a 20-resource environment in 2 minutes instead of 2 days

How Terraform Works: The Big Picture

Terraform follows a simple mental model:

  1. You write: describe the infrastructure you want in .tf files using HCL
  2. Terraform plans: compares your desired state to what currently exists and shows you exactly what will change
  3. Terraform applies: makes the API calls to create, update or delete resources
  4. Terraform records: writes the result to a state file so it knows what it manages

Terraform Core Workflow

.tf filesdesired state
terraform plandiff vs state
terraform applyAPI calls
State filerecorded reality

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:

FeatureTerraformCloudFormation
Cloud supportAny provider (AWS, Azure, GCP, …)AWS only
LanguageHCL (readable, concise)JSON or YAML (verbose)
State managementYou control the state fileAWS manages state
Error messagesGenerally clearNotoriously confusing
EcosystemMassive (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:

  1. Introduction (this page), what Terraform is and why it matters
  2. Installing Terraform: install on macOS, Linux or Windows and run your first config
  3. The Core Workflow: init, plan, apply, destroy in depth
  4. Variables and Outputs: make your code flexible and reusable
  5. State and Backends: understand the state file and use remote backends
  6. 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

Exercise

Question 1: What is Terraform?

Which of the following best describes what Terraform does?

Exercise

Question 2: The State File

Why does Terraform maintain a state file?

Exercise

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?

Exercise

Question 4: Declarative vs Procedural

Terraform is described as "declarative". What does this mean?


Frequently Asked Questions

What is Terraform used for?

Terraform provisions and manages infrastructure by describing it in configuration files rather than clicking through a console. You declare the resources you want, networks, servers, databases, DNS records, and Terraform works out what to create, change or destroy to make reality match. Because the configuration lives in version control, infrastructure changes get reviewed, diffed and rolled back the same way application code does, and an identical environment can be recreated on demand.

What is the difference between Terraform and Ansible?

Terraform is a provisioning tool: it creates and destroys infrastructure and tracks what exists in state. Ansible is a configuration management tool: it installs packages and configures software on machines that already exist. Terraform is declarative and works out the changes needed to reach your desired state; Ansible is primarily procedural, running tasks in order. They are complementary rather than competing, and a common pattern is Terraform to build the servers and Ansible to configure what runs on them.

Should I use Terraform or CloudFormation?

Terraform if you use more than one provider or want the skill to transfer between employers. It works across AWS, Azure, GCP, Kubernetes, Datadog, Cloudflare and hundreds of others with one language and one workflow. CloudFormation is AWS-only but needs no state file of your own, since AWS manages it, and StackSets deploy across an entire AWS Organization with no open-source Terraform equivalent. Many AWS teams run both: CloudFormation or CDK for account baselines, Terraform for application infrastructure.

Is Terraform still open source?

Terraform moved from the Mozilla Public License to the Business Source License in 2023, which restricts using it to build a competing commercial product but leaves normal infrastructure use unaffected. In response the community forked the last MPL version as OpenTofu, now under the Linux Foundation and largely a drop-in replacement. For most teams either works and the configuration language is the same; the choice comes down to licensing preference and which ecosystem your tooling targets.

Do I need to know a cloud provider before learning Terraform?

Yes, at least the basics of one. Terraform is a way to express infrastructure, not a substitute for understanding it. A aws_vpc resource still requires you to know what a VPC, a subnet and a route table are, and error messages come from the provider rather than from Terraform. Learn the fundamentals of one cloud first, then Terraform becomes a much faster way to work with what you already understand.

What is the hardest part of Terraform for beginners?

State. Terraform records what it created in a state file, and almost every confusing situation. A resource it wants to recreate for no obvious reason, a change made in the console that Terraform undoes, two people applying at once and corrupting the file. Traces back to state. Set up a remote backend with locking before working in a team rather than after, and spend time understanding what state is for early; it pays back more than any other Terraform concept.

Continue Learning

Explore Related Topics

Try the Tool

Practise this in the browser

Terraform Playground

Run init, plan, apply and destroy against simulated infrastructure.

When it breaks

Related Resources