Terraform reads configuration files, compares them to the state, builds a dependency graph of resource changes, and applies them in the order required by that graph.
The workflow has four stages. terraform init downloads the providers referenced by the configuration (each provider is a plugin that knows how to talk to a specific cloud API). terraform plan reads the configuration, reads the state, refreshes the state against the live APIs, and computes a diff. The diff is printed as a plan: what will be created, what will be updated, what will be destroyed. terraform apply executes that plan, respecting the dependency graph so that, for example, a subnet is created before the instance that uses it. terraform destroy reverses the operation, deleting everything the configuration manages.
graph LR
C[Config .tf files] --> I[terraform init]
I --> P[terraform plan]
S[State file] --> P
P -->|diff| R{Review}
R -->|approve| A[terraform apply]
A -->|API calls| Cloud[Cloud providers]
A --> S
The dependency graph is what allows Terraform to apply changes in parallel when they are independent. If the configuration defines a VPC, ten subnets inside it, and a load balancer that references the subnets, Terraform will create the VPC first, then the subnets in parallel, then the load balancer. That parallelism is bounded by the graph, and it is what makes a Terraform apply fast even when the configuration describes hundreds of resources.
Structuring Terraform for production
Split configuration into modules and environments
Create reusable modules for networks, compute, data, IAM. Create one environment per folder (staging, production) that instantiates the modules with its own variables.
Use a remote backend with locking
Configure the S3 + DynamoDB backend (or GCS, or Terraform Cloud) before writing any resource. Locking prevents two engineers from applying simultaneously.
Review the plan before every apply
Require a reviewer for any plan that destroys or replaces a resource. The plan is the review artifact; the configuration is not enough.
Pin provider versions
Lock provider versions in
.terraform.lock.hcland commit it. Provider upgrades can silently change resource behaviour; pin and upgrade deliberately.
Modern Terraform adds a state-locking mechanism (through the backend) that prevents concurrent applies from corrupting the state, and a terraform import command that adopts existing resources into management without recreating them. The terraform plan -out=plan.tfplan pattern saves the plan to a file and passes it to apply, guaranteeing that what was reviewed is what is applied — a critical safety property for automated pipelines.