Terraform Guide: Infrastructure as Code Explained, From First Apply to Teams

This Terraform guide explains how infrastructure as code works in practice. Terraform is an open-source tool from HashiCorp that lets teams describe cloud infrastructure in files, then create and change that infrastructure by applying those files.

The central question is simple. How do you build and change cloud infrastructure reliably, without clicking through consoles and hoping nothing gets missed? The answer is that you write the infrastructure down as code, preview exactly what will change before anything happens, and then apply the change in a repeatable way.

Key takeaways

  • Terraform describes infrastructure in code files, so the same setup can be created again and reviewed like any other code.
  • The plan step shows exactly what will change before anything is created, modified or destroyed.
  • The state file is Terraform’s record of what exists, and it must be shared and locked when teams work together.
  • Modules package common infrastructure into reusable pieces, and team workflows add remote state, review and automation.

What Terraform is

Terraform is an infrastructure as code tool, first released by HashiCorp in 2014. Infrastructure as code means describing servers, networks, databases and other cloud resources in text files, rather than creating them by hand in a web console. Those files can be stored in version control, reviewed in a pull request, and applied again to rebuild the same infrastructure from scratch.

Terraform works with many providers, including the major cloud platforms and services such as DNS providers, databases and monitoring tools. A provider is a plugin that teaches Terraform how to talk to a particular platform. The same workflow applies to all of them: write configuration, preview the change, then apply it.

The configuration language is HCL, the HashiCorp Configuration Language. It is designed to be readable, with blocks that describe resources and their settings. A resource block names the type of thing to create, such as a virtual machine or a storage bucket, and lists its settings, such as size, region and tags.

How Terraform works: plan, apply, destroy

Terraform’s workflow has three main commands, and understanding them is understanding the tool. Each one does one job, and together they form a loop that teams repeat for every change.

Plan: preview before you change anything

Running a plan asks Terraform to compare the configuration files with the real infrastructure and the state file, then report what would change. The output lists resources to create, modify or destroy, with the details of each change. Nothing is changed during a plan, which is what makes it safe to run at any time.

The plan is where mistakes are caught. A change that would delete a database or replace a server shows up in the output before it happens, giving the team a chance to stop and reconsider. Experienced teams treat the plan output as the thing to review, much as they would review a code change.

Apply: make the change

Applying runs a plan and then carries it out, creating, modifying or deleting resources in the right order. Terraform works out dependencies from the configuration, so a network is created before the servers that use it, and a database before the application that connects to it.

Apply asks for confirmation before proceeding, unless that prompt is disabled for automation. Once it finishes, Terraform writes the results into the state file, which becomes the record of what now exists.

Destroy: remove everything

Destroy is the reverse of apply. It removes every resource described in the configuration, which is useful for tearing down temporary environments, such as a test setup or a short-lived project. Because destroy deletes real infrastructure, teams usually guard it with confirmation steps and, for production, with rules that prevent it running at all.

Approach How infrastructure is created Best suited to Main trade-off
Manual console clicks Created by hand in a web console One-off experiments and learning Nothing is recorded or repeatable
Cloud-specific templates Declared in the provider’s own template language Teams using a single cloud only Locked to one provider’s syntax
Terraform Declared in HCL, applied through providers Teams managing infrastructure across providers State file must be managed carefully
Configuration management Software installed and configured on existing servers Managing what runs on servers Does not create the servers themselves
Cloud infrastructure diagram glowing on a dark monitor above a keyboard, with lime green network lines
A diagram of cloud infrastructure on screen shows how the resources Terraform manages connect to each other, which is why describing them in code is easier to review than clicking through a console.

The state file: Terraform’s memory

The state file is the part of Terraform that surprises newcomers most, and it is the part that causes the most trouble when mishandled. Terraform keeps a file, usually called terraform.tfstate, that records every resource it manages, along with the details it needs to update or delete them later. Without that record, Terraform would not know what already exists or what it is responsible for.

On a single computer, the state file can live on disk next to the configuration. That works for learning, but it breaks down as soon as a second person is involved, because two people cannot share a file on one laptop. Teams therefore store state remotely, in a shared location such as cloud storage, with a locking mechanism that stops two people applying changes at the same time.

State must also be treated as sensitive. It can contain values that should not be shared widely, such as passwords or keys Terraform read back from a resource. Access should be limited to the people and systems that apply changes. Losing a state file is serious, because Terraform then believes nothing exists and the next apply may try to create duplicates, so remote state should always have versioning or backups enabled.

Variables, outputs and modules

Three features turn a small Terraform setup into something reusable. Variables let the same configuration behave differently in different environments, such as a development setup with one small server and a production setup with several larger ones. A variable might set the region, the instance size or the environment name, and its value can come from a file, an environment variable or a command-line flag.

Outputs expose useful information after an apply, such as the address of a load balancer or the name of a storage bucket. Other tools and scripts can read outputs, which is how a deployment pipeline learns where the new infrastructure lives.

Modules are the third feature, and the most important for larger setups. A module is a folder of Terraform files that creates a related set of resources, such as a complete network or a database with its backups. Teams write modules once and reuse them across projects, which keeps configurations short and consistent.

How teams use Terraform together

Using Terraform alone is straightforward. Using it with a team needs a few shared habits, and the teams that struggle are usually the ones that skipped them. The first habit is storing configuration in version control, so every change is a pull request that somebody reviews, with the plan output attached for the reviewer to read.

The second habit is remote state with locking, so two applies cannot run at the same time and overwrite each other. The third is separating environments: development, staging and production should be separate Terraform setups, usually in separate folders or separate state files, so a mistake in one cannot affect another.

Many teams go further and automate the workflow. A pipeline runs a plan on every pull request and applies it when the change is approved, which means infrastructure changes go through the same review as application code.

Terraform and multi-cloud

One reason teams choose Terraform is that it works across providers. A team can manage resources in more than one cloud from the same codebase, using the same workflow and the same review process for each. That fits naturally with a multi-cloud strategy, where different workloads run on different providers.

It is worth being honest about the limits. Terraform does not make clouds interchangeable: a storage bucket, a load balancer and a database are different on each provider, and the configuration for each one is different too. What Terraform gives is a consistent workflow and a single place to see the whole estate. For larger organisations, that consistency is often the point, because it makes infrastructure changes reviewable and auditable across the whole estate.

Common mistakes and how to avoid them

The most common mistake is editing infrastructure by hand in a console after Terraform created it. Terraform will notice the difference on the next plan and try to undo it, which confuses everyone. The rule is simple: if Terraform manages a resource, change it through Terraform and nowhere else.

The second mistake is letting the state file become a single point of failure. State stored only on one person’s laptop, without backups, is a risk that only becomes visible when the laptop is lost. Remote state with versioning removes the risk for very little effort.

The third mistake is writing one enormous configuration for everything. Large files become hard to review and slow to apply, and a change to one small part carries the risk of touching everything else. Splitting infrastructure into smaller pieces, by environment or by service, keeps changes small and reviewable. Skipping the plan review is a fourth mistake, since the plan exists to catch destructive changes before they happen.

Checklist: starting with Terraform

  • Install Terraform and run a small apply against a test account before touching production.
  • Store configuration in version control and review every change as a pull request.
  • Run a plan and read its output before every apply, watching for deletions.
  • Move state to a remote backend with locking and versioning as soon as a second person is involved.
  • Separate development, staging and production into different state files or folders.
  • Use variables for values that change between environments, such as region and instance size.
  • Package repeated infrastructure into modules instead of copying configuration between projects.
  • Never edit Terraform-managed resources by hand in a console; change them through code.

Frequently asked questions

What is Terraform in simple terms?

Terraform is a tool that creates and manages cloud infrastructure from text files. You describe what you want, preview the change, and apply it, instead of building everything by clicking through a console.

What is infrastructure as code?

Infrastructure as code means describing servers, networks and databases in files that can be stored, reviewed and versioned like software. It makes infrastructure repeatable, so the same files can rebuild the same setup.

What is the Terraform state file?

The state file is Terraform’s record of the infrastructure it manages. Terraform reads it to work out what needs to change on the next plan or apply.

Is Terraform free?

Terraform’s core tool is open source and free to use. HashiCorp also offers paid services such as Terraform Cloud, which add remote state, collaboration and automation. In 2023, HashiCorp moved Terraform to the Business Source License, and the community created the OpenTofu fork under an open-source licence.

Do I need to know how to code to use Terraform?

No programming experience is needed. Terraform’s configuration language, HCL, is declarative and readable, and it is designed for describing infrastructure rather than writing programs. Familiarity with the cloud platform being used matters more than coding skill.

How is Terraform different from Ansible?

Terraform creates and manages infrastructure, such as servers and networks, while Ansible configures software on servers that already exist. Many teams use both: Terraform provisions the servers, and Ansible installs and configures the software on them.

The bottom line

Terraform turns infrastructure from a series of manual steps into a reviewed, repeatable process. Write the configuration, read the plan, apply the change, and let the state file keep track of what exists.

The discipline matters more than the tool. Keep state safe and shared, review every plan, never edit managed resources by hand, and split large setups into small pieces. Do that, and Terraform becomes the calm, predictable centre of how a team runs its cloud infrastructure.

Latest articles

Scroll to Top