Applying Changes
terraform apply makes the changes in a plan and records them in state. This lesson explains the two ways to apply, how state is updated and how failures are handled.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish applying with and without a saved plan.
- Explain how Terraform updates state during an apply and handles failures.
- Describe tainted resources and forced replacement.
Two ways to apply
Without a plan file, terraform apply does the work of plan first: it refreshes state, builds a plan, prints it and asks you to type yes. Any other answer cancels the run. Apply accepts the same planning options as plan, such as -var, -replace and -target. -auto-approve skips the prompt and belongs only in automation where the changes have been reviewed another way.
With a plan file, terraform apply tfplan performs exactly the saved actions and does not prompt, because approval happened when the plan was reviewed. If the state has changed since the plan was created, Terraform refuses to apply it as a stale plan, and you create a new one. This protects against applying a plan based on outdated information.
Applying safely
What happens during apply
Terraform walks the resource graph and asks each provider to make its changes, in parallel where dependencies allow. As each operation succeeds, Terraform writes the result to state, so state always reflects what has actually been done. When the apply finishes, Terraform prints a summary and the values of the root module's outputs.
If an operation fails, for example because of a quota limit or a permission error, Terraform stops starting new operations, waits for the ones in progress, saves state and reports the error. There is no automatic rollback: resources that were created successfully remain and are recorded. Fix the cause and run apply again; Terraform plans from the recorded state and completes the remaining work.
The following diagram shows the state an interrupted apply leaves and what the next apply does.

When apply fails
Tainted resources and forced replacement
If an object was created but a later part of its creation failed, such as a provisioner that runs a script, Terraform marks it tainted in state, because it cannot be sure the object is usable. The next plan shows it being replaced.
To replace a healthy-looking object that is misbehaving, such as a virtual machine with a corrupted disk, use terraform apply -replace="aws_instance.web". The plan shows the replacement for review before anything happens. This option replaces the deprecated terraform taint command, which changed state immediately without a plan.
Replacing an object
A partly failed apply
An apply that creates a network, two subnets and four instances fails after the network, the subnets and two instances are created, because the account's instance quota is reached. What is the state of things, and what should the engineer do?
The decisive facts are that state is updated per operation and there is no rollback.
State records the network, both subnets and the two instances that were created. Nothing is rolled back. After the quota is raised, running terraform apply again produces a plan with only the two missing instances, which Terraform creates. Running destroy or deleting state would be wrong and unnecessary.
Does terraform apply tfplan ask for confirmation?
Show answer
No. A saved plan was approved when it was reviewed, so apply performs it without prompting.
Exam Tips
Remember for the exam
- apply without a file plans and prompts; apply with a saved plan does not prompt.
- A saved plan is rejected as stale if state changed.
- No automatic rollback; fix the problem and apply again.
- -replace forces replacement and replaces terraform taint.