Terraform, created by HashiCorp and open-sourced in 2014, is the most widely adopted cloud infrastructure provisioning tool. It uses a declarative language (HCL), a provider plugin model, and a state file to manage infrastructure across hundreds of cloud platforms and services.
How Terraform Works
The Terraform workflow has four phases:
- Write: Define infrastructure in
.tffiles using HCL (HashiCorp Configuration Language) - Init:
terraform initdownloads providers and initialises the backend - Plan:
terraform plancompares desired state (code) to current state (state file) and shows what will change - Apply:
terraform applyexecutes the changes — creates, updates, or destroys resources
Providers
Terraform uses a provider plugin model. Providers are plugins that define the resources and data sources for a specific platform:
hashicorp/aws— 1,000+ AWS resourceshashicorp/google— GCP resourceshashicorp/azurerm— Azure resourceshashicorp/kubernetes— Kubernetes resourceshashicorp/helm— Helm releaseshashicorp/github— GitHub repositories, teams, webhooks- ...1,000+ providers on the Terraform Registry
Providers are downloaded from the Terraform Registry during terraform init.
Terraform State
Terraform maintains a state file (terraform.tfstate) that maps your configuration to real-world resources. State is how Terraform knows:
- Which resources it manages (vs resources that exist but weren't created by Terraform)
- What the current configuration of those resources is
- What needs to change when you run
plan
By default, state is stored locally. For teams, use a remote backend (S3, Terraform Cloud) to share state and enable locking.
Terraform vs Other IaC Tools
| Feature | Terraform | CloudFormation | Pulumi | Ansible |
|---|---|---|---|---|
| Multi-cloud | Yes | AWS only | Yes | Yes |
| Language | HCL | YAML/JSON | TS/Python/Go | YAML |
| State management | Yes (state file) | Managed by AWS | Yes | No (idempotent tasks) |
| Provisioning | Excellent | Excellent (AWS) | Excellent | Good |
| Config management | Limited | No | Limited | Excellent |
| Learning curve | Low | Medium | Depends on language | Low |
Terraform vs OpenTofu
In August 2023, HashiCorp changed Terraform's licence from MPL 2.0 (open source) to BSL (Business Source Licence), which restricts commercial use. In response, the community forked Terraform as OpenTofu, donated to the Linux Foundation, and maintained as a truly open-source alternative.
- OpenTofu 1.x is compatible with Terraform 1.5.x syntax
- Both tools use the same HCL syntax and provider ecosystem
- OpenTofu is the tool most open-source projects are migrating to
For learning purposes, HCL skills transfer directly between Terraform and OpenTofu.
Terraform CLI Commands Overview
terraform init # initialise directory, download providers
terraform validate # check syntax and configuration
terraform fmt # format code to canonical style
terraform plan # preview changes
terraform apply # apply changes
terraform destroy # destroy all managed resources
terraform output # show output values
terraform state list # list resources in state
terraform import # import existing resource into state
terraform workspace # manage named workspaces
Next: HCL — the language you write Terraform configurations in.