Terraform Explained: Infrastructure as Code for Cloud Teams

Terraform Explained: Infrastructure as Code for Cloud Teams

Terraform explained simply: Terraform is an infrastructure as code tool that lets you define cloud resources — servers, networks, databases and storage — in text files, and then creates and manages those resources for you. Instead of clicking through a cloud console, you write down what you want, and Terraform makes it so. This guide explains how Terraform works, what its key concepts mean, and when it is the right choice.

The central question is why teams manage infrastructure with code at all. The answer is repeatability: a text file can be reviewed, versioned, tested and reused, while a console click cannot. If you can rebuild an entire environment from files in a repository, you can recover from disaster, clone environments and review changes the way you review application code.

Key takeaways

  • Terraform describes infrastructure declaratively in HashiCorp Configuration Language (HCL): you state the desired state, and Terraform works out how to reach it.
  • State is Terraform’s record of what it manages; protect it with a remote backend and locking.
  • The plan step previews every change before anything is applied, which makes reviews meaningful.
  • Providers connect Terraform to cloud platforms and services, and modules package reusable patterns.
  • Since 2023, Terraform releases have shipped under the Business Source Licence, and OpenTofu is the community fork under the Mozilla Public Licence.

What Is Terraform?

Terraform is an open-source infrastructure as code tool created by HashiCorp and first released in 2014. It works across cloud providers: the same tool can manage resources on Amazon Web Services, Microsoft Azure, Google Cloud Platform and many other platforms, as well as software services such as databases, monitoring tools and version control systems.

The core idea is declarative. You write configuration files that describe the infrastructure you want — a virtual network, three servers, a database with a specific size — and Terraform compares that description with the real world.

It then creates, updates or deletes resources until reality matches the files. You describe the destination, not the journey.

That model contrasts with imperative scripts, which list the steps to run in order. A script that creates a server works once; running it twice may create two servers, or fail. Terraform’s declarative approach can run repeatedly and converge on the same result, which is what makes it safe to use in a pipeline.

HCL: The Language of Terraform

Terraform configuration is written in HashiCorp Configuration Language, or HCL. HCL is a declarative language designed for describing infrastructure, with a syntax that is readable even for people who do not write code daily. A resource block names the type of resource, gives it a local label, and lists its settings.

A typical configuration is short. A block might declare a storage bucket with a name and a region, or a virtual machine with a size and an image. Variables let the same configuration adapt to different environments, outputs expose values such as an address or an identifier for other systems to use, and data sources let Terraform read existing resources without managing them.

State: Terraform’s Memory

State is the record Terraform keeps of the resources it manages. Each resource in the state file maps a block of configuration to a real cloud resource, identified by its cloud provider identifier. When Terraform runs, it reads the state, compares it with the configuration, and asks the provider what the real resources look like now.

By default, state lives in a local file, which is fine for learning but not for teams. A local state file cannot be shared, cannot be locked, and is easily lost.

Production use needs a remote backend: a shared store such as an object storage bucket, with locking so that two people cannot apply changes at the same time. Without locking, concurrent runs can corrupt the state and leave Terraform confused about what exists.

State contains sensitive data. Resource attributes can include passwords, keys and connection strings, so the backend must be encrypted and access must be restricted. State should never be committed to version control, and nobody should edit it by hand unless they understand exactly what they are changing.

Plan, Apply and the Terraform Workflow

The daily Terraform workflow has three steps. First, you write or edit configuration. Second, you run terraform plan, which shows the changes Terraform would make: resources to create, update, replace or destroy.

Third, you run terraform apply to make those changes real. The plan is the reviewable artefact, and in a team it should be inspected before anything is applied.

Two commands round out the workflow. terraform refresh updates the state to match the real world, which is useful when someone has changed a resource outside Terraform.

terraform destroy removes everything the configuration manages, which is handy for temporary environments and dangerous anywhere else. Our guide to infrastructure as code for repeatable environments explains how this workflow scales to larger, more complex estates.

Laptop showing infrastructure code in an editor with a terminal window beside it
Terraform configuration is plain text, which means it can be reviewed, versioned and tested like any other code.

Providers and Modules

Providers are the plugins that connect Terraform to a platform. Each provider knows how to talk to one cloud or service: the AWS provider manages Amazon resources, the Azure provider manages Microsoft resources, and there are providers for databases, monitoring, DNS and dozens of other services. A configuration declares which providers it needs, and Terraform downloads them automatically.

Modules package reusable configuration. A module is a folder of Terraform files that creates a related set of resources, such as a complete virtual network or a standard database setup, with variables for the parts that change. Teams publish modules for their common patterns, so a new service can reuse a proven network or database module instead of copying configuration by hand.

The public Terraform Registry hosts thousands of modules and providers written by HashiCorp, cloud vendors and the community. Registry modules are a good starting point, but teams should review them before use: a module that creates public storage by default is a risk, not a shortcut. Pin module versions so that an update to a shared module does not silently change your infrastructure.

Terraform, Licences and OpenTofu

Terraform’s licence changed in August 2023, and any honest explanation needs to cover it. Until version 1.5, Terraform was released under the Mozilla Public Licence 2.0, an open-source licence. From version 1.6 onwards, HashiCorp ships Terraform under the Business Source Licence 1.1, which is source-available rather than open source: the code is visible, but the licence restricts using it to offer a competing commercial service.

The change prompted a community fork. OpenTofu is a fork of Terraform created from the last Mozilla-licensed release, governed by the Linux Foundation and released under the Mozilla Public Licence 2.0.

It remains compatible with most Terraform configuration and state, and it has since added features of its own, such as state encryption. IBM completed its acquisition of HashiCorp in February 2025, and Terraform is now an IBM product.

Terraform in a CI/CD Pipeline

Terraform works best when it runs in a pipeline, not on individual laptops. A pipeline gives every change the same treatment: format checking, validation, a plan posted to the pull request, and an apply step that runs only after approval on a protected branch. That consistency is what makes infrastructure changes safe at scale.

Secrets and credentials deserve care here. The pipeline needs cloud credentials with the minimum permissions required, stored in the pipeline’s secret store, and Terraform variables that hold secrets should be marked sensitive. State access should be limited to the pipeline and a small number of administrators.

Drift is the problem pipelines solve over time. Someone changes a resource in the cloud console, and the next plan shows Terraform proposing to undo it.

Teams should decide in advance whether console changes are allowed at all, and use regular plans to detect drift early. The GitOps approach, where Git is the single source of truth, is described in our guide to GitOps, where Git becomes the source of truth for infrastructure.

Terraform compared with CloudFormation and Pulumi
Feature Terraform CloudFormation Pulumi
Language HCL YAML or JSON TypeScript, Python, Go, C#, Java
Cloud support Multi-cloud via providers AWS only Multi-cloud via SDKs
Licence Business Source Licence since 2023 Proprietary AWS service Apache 2.0 for CLI and SDKs
State State file, remote backends Managed as stacks by AWS State file or Pulumi Cloud
Module ecosystem Largest registry of modules Smaller, AWS-focused Growing; can use Terraform modules
Best for Multi-cloud teams wanting a declarative language AWS-only estates Engineering-led teams who code daily

Best for: teams managing infrastructure across more than one cloud, or teams that want a declarative language with the largest module ecosystem.

Common Terraform Mistakes

The most common mistake is managing state carelessly. Local state on a laptop, state committed to version control, or a backend without locking will eventually cause a painful incident. Set up a remote backend with locking and encryption before the first production resource is created.

Another mistake is applying changes without reviewing the plan. A plan that shows a database being replaced rather than updated is a warning, and applying it blindly can delete data. Treat the plan as the change itself: review it, question it, and never apply a plan you have not read.

Checklist: Using Terraform Well

  • Store state in a remote backend with locking and encryption, never in version control.
  • Review the plan before every apply, and automate plans on pull requests.
  • Pin provider and module versions so updates are deliberate.
  • Use variables and outputs instead of hard-coding values.
  • Split large configurations into focused units and reuse modules.
  • Run Terraform in a pipeline with minimal-permission credentials.
  • Detect drift with regular plans and decide whether console changes are allowed.
  • Decide deliberately between Terraform and OpenTofu, based on licence and roadmap.

Frequently Asked Questions

Is Terraform free to use?

Yes, for most teams. The Terraform command-line tool is free to download and use inside your own organisation, and the Business Source Licence only restricts offering it as a competing commercial service. HashiCorp’s managed platform, HCP Terraform, is a separate paid product.

What is Terraform state?

State is Terraform’s record of the resources it manages, mapping configuration blocks to real cloud resources. It lets Terraform work out what needs to change, and it must be stored remotely with locking for team use.

Does Terraform work with all cloud providers?

Terraform works with the major clouds — AWS, Azure and Google Cloud — through providers, plus hundreds of other platforms and services. A provider exists for most services a team is likely to use, and custom providers can be written when one does not.

What is the difference between Terraform and OpenTofu?

OpenTofu is a community fork of Terraform under the Linux Foundation, released under the Mozilla Public Licence 2.0. It is largely compatible with Terraform configuration and state, and has added features such as state encryption, while Terraform continues under HashiCorp and IBM.

Can Terraform manage existing infrastructure?

Yes. Terraform can import existing resources into its state, so a resource created by hand or by another tool can be brought under Terraform management. Importing takes care, because the configuration must then match the real resource.

Is Terraform hard to learn?

The basics are straightforward: a small configuration, a plan and an apply can be learned in a day. The depth comes with state management, modules and pipelines, which are skills worth building gradually on real projects rather than all at once.

Terraform explained in one sentence: you write down the infrastructure you want, and Terraform makes it real, repeatably. Learn the workflow, protect the state, review every plan, and it becomes one of the most reliable tools in a cloud team’s kit.

Latest articles

Scroll to Top