DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Terraform Tutorial

Introduction to Terraform
Installing Terraform
The Terraform Core Workflow
Terraform Variables and Outputs
Terraform State and Backends
Terraform Modules
Terraform Data Sources
Terraform Count and for_each
Terraform Expressions and Functions
Terraform Dynamic Blocks
Terraform Workspaces
Terraform with AWS
Terraform Provisioners
Terraform CI/CD Pipeline
Terraform Security

Introduction to Terraform

Next

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

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?

Next

Continue Learning

Installing Terraform

Step-by-step guide to installing Terraform on macOS, Linux and Windows using the official HashiCorp package managers. Verify the install and run your first real Terraform configuration.

10 min read·Easy

The Terraform Core Workflow

The four commands that power every Terraform project, init, plan, apply, and destroy. Learn the complete workflow loop used by every DevOps engineer managing infrastructure as code.

5 min read·Easy

terraform init

Learn exactly what terraform init does, downloading providers, setting up backends, creating the lock file, and how to use init flags like -upgrade and -reconfigure.

10 min read·Easy

Explore Related Topics

AW

AWS Tutorials

Provision AWS infrastructure with Terraform

CI

CI/CD Tutorials

Run terraform plan/apply in CI pipelines

Try the Tool

YAML Validator

Validate Terraform and Kubernetes YAML configs before applying.

On This Page

What Is Terraform?What Is Infrastructure as Code?The problem IaC solvesWhat IaC gives youHow Terraform Works: The Big PictureCore Concepts You Will Use Every DayProvidersResourcesData SourcesVariables and OutputsTerraform vs Other IaC ToolsTerraform vs AnsibleTerraform vs AWS CloudFormationTerraform vs PulumiThe Terraform Workflow in One MinuteWhat This Series CoversKnowledge Check