Terraform
An open-source IaC tool by HashiCorp that provisions infrastructure across any cloud provider using declarative HCL configuration. Plan, apply, destroy.
What is Terraform?
In short
Terraform is an open-source infrastructure-as-code tool from HashiCorp that lets you define cloud and on-premise resources in declarative configuration files and create, change, or delete them with a predictable workflow. You write what you want the infrastructure to look like in HCL, Terraform compares that to the current real state, and it makes only the changes needed to match.
What Terraform actually is
Terraform is a command-line tool that turns text files into running infrastructure. Instead of clicking through the AWS console to create a server, a database, and a load balancer, you describe them in a file and run one command. Terraform talks to the provider's API and builds everything for you.
The configuration language is called HCL (HashiCorp Configuration Language). It is declarative, which means you describe the end result you want, not the step-by-step instructions to get there. You say I want three EC2 instances and an S3 bucket, and Terraform figures out the order to create them and the dependencies between them.
Terraform is provider-agnostic. The same tool manages AWS, Google Cloud, Azure, Cloudflare, Datadog, GitHub, Kubernetes, and over 3,000 other services through plugins called providers. One workflow covers your whole stack, not just one vendor.
How it works under the hood
The core of Terraform is the state file, usually named terraform.tfstate. It is a JSON record of every resource Terraform created and the real-world IDs that map to your config. When you run terraform plan, it reads your config, reads the state, queries the live providers, and computes the difference. The output is an exact list of what it will add, change, or destroy before anything happens.
terraform apply executes that plan. Terraform builds a dependency graph so it knows that a subnet must exist before the instance that lives in it, and it parallelizes everything that has no dependency on each other. terraform destroy walks the graph in reverse to tear it all down.
Because the state file is the source of truth, teams almost never keep it on a laptop. They store it in a remote backend like an S3 bucket with DynamoDB locking, Terraform Cloud, or Google Cloud Storage. The lock stops two engineers from running apply at the same time and corrupting the state. Sensitive values like passwords end up in this file in plain text, so the backend must be encrypted and access-controlled.
When to use it and the trade-offs
Reach for Terraform when you provision infrastructure repeatedly, across environments, or as a team. Spinning up identical staging and production setups, version-controlling your network design, and reviewing infrastructure changes in a pull request are all things Terraform does well. The plan output alone, showing exactly what will change before it changes, prevents a lot of 2 AM mistakes.
The main trade-offs are state management and drift. The state file is fragile. If someone changes a resource by hand in the console, Terraform sees drift on the next plan and may try to undo it. Importing existing resources into state is tedious. And HCL is good at provisioning but weak at procedural logic, so complex loops and conditionals get awkward.
Terraform provisions infrastructure but does not configure what runs inside it. It will create a server, but installing and starting your application is a job for Ansible, cloud-init, or a container image. People often confuse Terraform with configuration management tools like Ansible or Chef. They solve different parts of the problem and are frequently used together. Note that since version 1.6 HashiCorp moved Terraform to the Business Source License, which prompted the community fork OpenTofu, a drop-in open-source replacement.
A concrete example
Suppose a startup runs the same web app in dev, staging, and production. Without Terraform, an engineer clicks through three sets of VPCs, subnets, security groups, RDS databases, and EC2 instances, and the three environments slowly drift apart. With Terraform, the whole stack lives in a few .tf files, and a variable switches the environment name and instance size.
To launch a fresh staging environment, the engineer runs terraform workspace select staging, then terraform plan to preview, then terraform apply. In a couple of minutes the entire environment exists and is byte-for-byte identical in shape to production. To save money overnight, terraform destroy removes it, and the same apply rebuilds it tomorrow.
Reusable pieces are packaged as modules. A team might publish a vpc module and a database module, then every project consumes them with a few lines, so a network change made once propagates everywhere it is used. This is how companies manage thousands of resources without losing track of any of them.
Where it is used in production
Amazon Web Services
The most common Terraform provider; teams provision VPCs, EC2, S3, RDS, and IAM declaratively instead of clicking the console.
Kubernetes
Terraform provisions managed clusters such as EKS, GKE, and AKS, and the Kubernetes provider can manage namespaces and deployments inside them.
Cloudflare
DNS records, page rules, WAF settings, and Workers are managed as code through the official Cloudflare Terraform provider.
HashiCorp
The creator of Terraform; HashiCorp Terraform Cloud and Terraform Enterprise host remote state, run plans on shared workers, and enforce policy.
Frequently asked questions
- What is the difference between Terraform and Ansible?
- Terraform provisions infrastructure (creates the servers, networks, and databases) and is declarative. Ansible configures what runs inside that infrastructure (installs packages, edits config files, starts services) and is procedural. They overlap a little but are usually used together: Terraform builds the box, Ansible sets it up.
- What is the terraform.tfstate file and why does it matter?
- It is the JSON record that maps your config to the real resources Terraform created. Terraform reads it on every plan to know what already exists. If you lose or corrupt it, Terraform forgets it owns your infrastructure. Always store it in a remote, encrypted, locked backend like S3 with DynamoDB, never just on a laptop or in Git.
- Is Terraform still open source?
- As of version 1.6 in 2023, HashiCorp changed Terraform from the Mozilla Public License to the Business Source License, which restricts commercial competing use. In response the community created OpenTofu, a fully open-source fork under the Linux Foundation that is a drop-in replacement for most use cases.
- What does terraform plan do?
- It performs a dry run. Terraform reads your config and the current state, queries the live providers, and prints the exact set of resources it would add, change, or destroy, without touching anything. You review that output, then run terraform apply to make it real.
- What is a Terraform module?
- A module is a reusable, self-contained package of Terraform config, for example a standard VPC or database setup. You write it once and call it from many places with different inputs, which keeps configurations consistent and lets a single change propagate everywhere the module is used.
Learn Terraform hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Terraform as part of a larger topic.
Infrastructure as Code (IaC)
Defining servers, networks, databases, and cloud resources in declarative code files instead of clicking through consoles
intermediate · devops cicd
Infrastructure as Code
Define infrastructure in version-controlled code. Terraform, Pulumi, and reproducible environments
advanced · reliability resilience
See also
Related glossary terms you might want to look up next.
Infrastructure as Code
Managing servers, networks, and cloud resources through declarative configuration files instead of manual setup. Terraform, Pulumi, and CloudFormation are IaC tools.
Ansible
An agentless automation tool for configuration management, application deployment, and orchestration. Uses YAML playbooks and connects over SSH.
GitOps
Using Git as the single source of truth for infrastructure and application configuration. Changes are made via pull requests and automatically reconciled by tools like ArgoCD or Flux.
Virtual Machine
A software emulation of a physical computer running its own OS on shared hardware. Heavier than containers but provides stronger isolation.
Hypervisor
Software that creates and manages virtual machines by abstracting physical hardware. Type 1 (bare-metal) runs directly on hardware; Type 2 runs on an OS.
Cloud Region
A geographic area containing one or more data centers (availability zones). Choosing the right region reduces latency and satisfies data residency requirements.