Terraform
Overview
Terraform is HashiCorp's infrastructure-as-code tool that lets you define cloud and on-prem infrastructure declaratively in HashiCorp Configuration Language (HCL). It computes an execution plan to reach the desired state and applies it through provider plugins, tracking real-world resources in a state file. Read this when authoring .tf configuration, managing state, or running the plan/apply lifecycle.
Official sources
- Docs: https://developer.hashicorp.com/terraform/docs
- Repo: https://github.com/hashicorp/terraform
- Install: https://developer.hashicorp.com/terraform/install
Install / setup
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Source: https://developer.hashicorp.com/terraform/install
Core concepts
- HCL — declarative configuration language used in
.tffiles to describe resources. - Provider — plugin (e.g.
aws,azurerm) that maps HCL to a target API. - Resource — a single infrastructure object (
resource "aws_instance" "web" {}). - State — recorded mapping of config to real resources (
terraform.tfstate); source of truth for diffs. - Plan / apply —
planpreviews changes;applyenacts them to converge to desired state. - Module — reusable, parameterized grouping of resources for composition.
- Variable / output — inputs (
variable) and exported values (output) for configurability. - Backend — where state is stored (local, S3, Terraform Cloud) enabling remote/locked state.
Best practices
- Store state remotely with locking (e.g. S3 + DynamoDB, Terraform Cloud) for team safety (https://developer.hashicorp.com/terraform/language/backend).
- Always review
terraform planoutput beforeapply(https://developer.hashicorp.com/terraform/cli/commands/plan). - Pin provider and module versions with constraints to ensure reproducible runs (https://developer.hashicorp.com/terraform/language/providers/requirements).
- Structure reusable infrastructure as modules with explicit inputs/outputs (https://developer.hashicorp.com/terraform/language/modules).
- Run
terraform fmtandterraform validatein CI before merging (https://developer.hashicorp.com/terraform/cli/commands/fmt).
Common pitfalls
- Committing
terraform.tfstate(often with secrets) to git → use a remote backend and gitignore local state. - Manual ("click-ops") changes drift from config → run
terraform planto detect drift and reconcile. - Unpinned versions cause surprise upgrades → set
required_providersandrequired_versionconstraints.
Examples
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
}
provider "aws" { region = "us-east-1" }
resource "aws_s3_bucket" "data" {
bucket = "my-app-data-bucket"
}
Further reading
- https://developer.hashicorp.com/terraform/language — HCL language reference
- https://registry.terraform.io/ — providers and modules registry
- https://developer.hashicorp.com/terraform/cli/commands — full CLI command reference
Related skills
- ../ansible — configure resources after Terraform provisions them
- ../azure — target cloud provisioned with the azurerm provider