What Happens on terraform apply

Config, state, plan, providers

Follow one terraform apply end to end: the files it reads, the state lock and refresh, the dependency graph and plan, the provider calls made in parallel, and what is left behind when a step fails.

Overview

terraform apply makes real infrastructure match the configuration in a directory. It is a short command with a long path behind it: read every .tf file, lock the state, ask the cloud what really exists, work out the order of changes, show a plan, then call cloud APIs, often in parallel, and record each result.

One correction comes first, because the question is often asked as terraform apply -f file.tf. Terraform has no -f flag and never applies a single file. It loads every .tf and .tf.json file in the working directory as one configuration. This is what Terraform 1.16 prints:

The interviewer listens for four things. Do you know what Terraform reads and what init set up before? Can you explain state: why it exists, why it is locked, and what refresh does with it? Can you say how the plan decides between update and replace, and in what order changes run? And do you know what happens when the run fails halfway, since nothing is rolled back?

Questions to ask first

Agree on the setting before you walk through the command. Here are the questions, each with the answer this walkthrough assumes.

  • Where is state kept? In a remote S3 backend shared by the team, with locking on.
  • Who runs apply? A CI pipeline, applying a plan that was reviewed. Engineers run plan locally.
  • Which providers? One cloud provider plus a few utility providers.
  • How big is the configuration? About 300 resource instances in one root module, with 20 changing per apply (assumptions).
  • Has init run? Yes, terraform init has run, and the dependency lock file is committed.

The design at a glance

One apply runs in three phases, drawn below. Prepare loads every .tf file, locks the state, reads it, and refreshes it from the cloud APIs, so Terraform knows the configuration and the real world. Plan builds the dependency graph, diffs the configuration against the state, and waits for approval. Apply walks the graph, 10 resources at a time, while each provider calls the cloud APIs; then Terraform writes the state and unlocks it. The sections below follow the same order.

What you will be able to do

  • Say exactly which files terraform apply reads, the real forms of the command, and what init prepared.
  • Explain state, the lock, refresh and drift, and recover from a stale lock.
  • Read a plan: every action symbol, why something is replaced instead of updated, and the order of changes.
  • Estimate how long refresh and apply take from the graph and the parallelism limit.
  • Explain what a failed apply leaves behind and how to continue safely, and run apply from CI on a reviewed plan.

Key concepts

  • Working directory
  • Dependency lock file
  • Backend and state
  • State locking
  • Refresh and drift
  • Dependency graph (DAG)
  • Plan actions
  • Provider plugins
  • Tainted resources
  • Saved plans

Functional requirements

  • Load the whole configuration of the working directory: resources, data sources, variables, locals, modules, outputs
  • Run with the provider versions recorded in the dependency lock file
  • Allow only one operation at a time to change a given state
  • Detect changes made outside Terraform (drift) before planning
  • Show the exact set of changes and wait for approval, or apply a saved plan that was already approved
  • Create, update, replace and destroy resources in dependency order, in parallel where independent
  • Record every resource that exists after the run, including after a failure
  • Print outputs and release the lock when done

Non-functional requirements

  • Repeatable: applying the same configuration twice makes no changes the second time
  • Safe under concurrency: two runs never write the same state at once
  • Recoverable: a run that fails or is interrupted leaves a state that can be brought back in line with what was created
  • Reviewable: what is applied in production is exactly the plan that was reviewed
  • Auditable: who changed what is visible in version control, the CI log and the state history
  • Least privilege: the credentials used by apply can change only what this configuration manages

Interview questions

  • Can you apply a single .tf file?
  • Why does Terraform need a state file?
  • What happens if two people run apply at the same time?
  • Why does the plan say 'must be replaced' instead of updating?
  • Apply failed halfway. What state is the infrastructure in?
  • What does refresh do, and what is drift?
  • How does Terraform decide what runs in parallel, and how long will it take?
  • How is terraform apply different from kubectl apply?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio