Using terraform plan and apply Safely
Fast recall
Quick revision
Core answer
Whether you follow safe practices: reviewing plan output before applying, using saved plan files, running in CI with approvals, and understanding the risk of destructive changes.
Running `terraform apply` without reviewing the plan, or applying directly from a developer laptop in production rather than from a CI/CD pipeline with controlled credentials.
Mention using `-out` to save the plan file and guarantee the apply executes exactly the reviewed plan, using `CDKTF` or Atlantis for PR-based Terraform workflows, and spotting replace/destroy operations in plan output.
Interview-ready guide
Interview answer
terraform plan shows exactly what Terraform will do before it does anything — it is the safety check that separates infrastructure as code from running aws cli commands against production. Reviewing the plan before apply is not optional; it is the entire point of the workflow.
Reading a plan output
- The plan shows three action symbols:
+for create,-for destroy,~for modify in place; a~/means destroy and recreate — the most dangerous because it means downtime for the replaced resource - Any
~on a resource with production traffic is a reason to pause and understand why — a change to an RDS instance'sidentifierdestroys and recreates the database; a change tomulti_azmodifies it in place; these look similar in HCL but have completely different operational impact - Use
terraform plan -out=tfplanto save the plan as a binary file;terraform apply tfplanapplies exactly that plan without re-computing it — the infrastructure state cannot change between your plan review and the apply in CI
Targeted applies and drift
terraform apply -target=aws_instance.app_serverapplies changes to one resource; use this only as a rescue operation for stuck resources — regular use of-targetleaves the rest of the configuration unapplied and creates drift between the configuration and realityterraform plan -refresh-onlyupdates state to reflect real infrastructure without making changes — the correct first step when you suspect manual changes have been made to infrastructure outside of Terraform; it shows what Terraform thinks differs from reality- Drift (manual changes made outside Terraform) is the most common production incident pattern with Terraform: a manual security group rule gets overwritten on the next apply; run
terraform planin CI regularly to detect drift before an emergency forces an apply
CI/CD integration
- In CI, the plan runs on every PR and posts the output as a PR comment — reviewers see the infrastructure impact of code changes without running Terraform locally
- The apply runs only after merge to main, after a second plan confirms the state has not changed since the PR plan — the two-plan-one-apply pattern catches state changes that happen between PR approval and merge
Using terraform plan and apply Safely
Direct Answer
Always review plan output for destroy/replace operations before applying; save the plan file with -out to guarantee apply matches the reviewed plan; run in CI with approval gates for production.
Common Mistakes & Pitfalls
Running terraform apply without reviewing the plan, or applying directly from a developer laptop in production rather than from a CI/CD pipeline with controlled credentials.
Check your understanding
Answer these without looking back at the article.
- How do you prevent accidental resource destruction in Terraform?
- What is Atlantis and how does it improve Terraform workflows?
- How do you handle a failed terraform apply mid-way through?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.