Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

What is Terraform?

9 min read
Improve this page

Logo Terraform

You have just heard about Terraform and you want to understand what it is in practice. The shell scripts you use today work, but they have no memory: running the script twice creates the resource twice, or fails. Every environment ends up drifting away from the others.

Terraform is an Infrastructure as Code (IaC) tool: you describe the wanted state of your infrastructure (virtual machines, networks, DNS) in files, and Terraform creates, changes or deletes whatever is needed to reach it. Before each change, it compares that wanted state with the real infrastructure and applies only the difference. It works in the cloud as well as on your own servers, wherever a provider exists: a plugin that talks to the API of the target technology. The file where Terraform records what it has created (the state) and the preview of the changes (the plan) are introduced further down.

What you will learn

  • What Terraform is: a simple definition and its place in the DevOps ecosystem
  • Which problem it solves: why scripts and clicking around are no longer enough
  • What it does and does not do: the scope of the tool and the limits to know
  • The key concepts: provider, resource, state, plan, the four pillars
  • When to choose Terraform: a comparison with shell scripts

Why does Terraform exist?

Before IaC tools, creating an infrastructure looked like this:

  1. Log in to the cloud console.
  2. Click to create a VM, configure the network, attach a disk.
  3. Document the steps and hope your colleague follows the same process.
  4. Start over for each environment (dev, staging, prod).

The result: environments that drift apart, human errors, and infrastructures that cannot be faithfully recreated.

Terraform solves these problems by making the infrastructure declarative (you describe the result you want, not the steps to get there), versionable (the files go into Git) and idempotent.

What does Terraform do?

The diagram follows one change from end to end: the developer describes the wanted state in HCL files (the configuration language of Terraform), Terraform applies it to the infrastructure, then the state, the state file, keeps the correspondence between the code and the real world.

From the HCL configuration to the real infrastructure: the developer describes the wanted state in HCL files, Terraform applies them through plan then apply to create, change or delete the infrastructure, and the terraform.tfstate file saves the state that links it to the code.describes the desired stateconfigurationcreates / updates / deleteswriteslinks to the codeDeveloper/ OpsHCL filesmain.tf • variables.tf • outputs.tfTerraformplan → applyInfrastructureVMs • Networks • DNS • Firewallterraform.tfstatesaved stateyouversioned codeTerraformreal infrastructurestate filewrites or reads the codeAPI callstracked by the state
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

Terraform handles the complete lifecycle of an infrastructure:

  • Create: provision new resources (VMs, networks, DNS...).
  • Change: update existing resources by changing their configuration.
  • Destroy: delete resources in a controlled way.
  • Plan: show the planned changes before applying them.
  • Remember the state: keep the correspondence between your code and the real infrastructure in a state file.

What does Terraform not do?

Terraform does not replace:

  • Ansible: for configuring servers after provisioning.
  • Kubernetes: for orchestrating containers.
  • Docker: for building images.
  • CI/CD: for deploying applications.

It integrates with all of these tools but is not meant to replace them.

What are the key concepts?

Terraform rests on four fundamental concepts:

ConceptShort definition
ProviderPlugin that connects Terraform to an API (AWS, libvirt, Kubernetes...)
ResourceObject managed by Terraform (a VM, a network, a bucket...)
StateFile that maps the real resources to the configuration
PlanPreview of the changes before they are applied

The Terraform workflow shows the plan and the state in action. The page Providers, resources and data sources details the first two concepts.

What can you manage with Terraform?

  • Libvirt/KVM: create local VMs, virtual networks, disk volumes.
  • AWS: provision EC2 instances, VPCs, S3 buckets, RDS databases.
  • Kubernetes: deploy namespaces, ConfigMap objects, Service objects.
  • GitHub: manage repositories, teams, branch protection rules.
  • DNS: create DNS records at Cloudflare, OVH, or Route 53.

A single tool covers all these cases: the providers are found on the Terraform Registry.

Terraform vs shell scripts

A shell script is enough to start automating an infrastructure. The table shows what it does not do on its own.

CriterionShell scriptTerraform
ReproducibleRarelyYes, with pinned Terraform and provider versions
IdempotentHas to be implemented by handBuilt in
Change detectionNoYes (plan)
Built-in destroyNoYes
Dependency managementManualAutomatic
Tracking the state of resourcesNoThrough the state

A shell script that creates a VM does not know whether it already exists, unless you write that test yourself. Terraform knows, thanks to the state. A script stays suitable for a one-off task. Terraform becomes useful as soon as you have to recreate, change or destroy the same infrastructure several times.

A short history of Terraform

YearEvent
2014First public release by HashiCorp
2017Terraform 0.10: providers split out
2021Terraform 1.0: v1.x compatibility promise (language, CLI workflow, provider protocol)
2023License change (BSL 1.1) → creation of OpenTofu
2024Terraform 1.8: provider functions
2026Terraform 1.17: current stable branch

Put it into practice

This lab needs Terraform: install it first with the lesson Install Terraform, then start the lab. It works on a purely local configuration. You apply the same configuration twice to see that the second apply changes nothing. You then change a resource outside Terraform: this gap with the code is called drift, and the plan detects it. Finally you read the state to see that it keeps the memory of what Terraform has created.

Key takeaways

  • Terraform describes the wanted infrastructure in .tf files.
  • It plans the changes, applies them, and remembers the resulting state.
  • It does not configure the inside of servers: prefer Ansible, cloud-init or Packer for that.
  • A provider connects Terraform to each technology (libvirt, AWS, GCP...).
  • The state is the file that maps your code to the real infrastructure.