Terraform interview question · Most asked
Terraform apply fails halfway through — some resources were created, others were not. How do you recover?
Scenario Asked in DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE) interviews
Summary
Don't panic and don't re-run blindly. Terraform records each resource it successfully
created in state as it goes, so the state file already reflects the partial progress.
Run terraform plan again — it will show a plan that creates only the remaining
resources and leaves the already-created ones alone.
Longer Answer
Terraform updates the state file after each resource operation, not at the end. If apply creates resources A, B, and C in order and fails on C:
- A and B are in state — Terraform knows they exist and won't try to recreate them.
- C is not in state — the next plan will attempt to create it again.
Recovery steps:
- Read the error message. The most common causes are: API rate limits, permission errors, resource limits (e.g. max VPCs), invalid arguments that the provider couldn't validate at plan time, or a dependency that doesn't exist yet (eventual consistency).
- Fix the root cause. Request a limit increase, fix the IAM policy, correct the argument.
- Run
terraform plan. It shows only the remaining changes. Review them. - Run
terraform apply. It picks up where it left off.
Edge cases:
- Resource created but not recorded in state (rare — Terraform crashed between the
API call and the state write): the next apply creates a duplicate. Fix with
terraform importfor the orphan andterraform state rmfor the duplicate address. - Resource partially created (e.g. an RDS instance that started provisioning but failed): check the AWS console. If the resource exists in a failed state, delete it manually or import it and let Terraform recreate it.
- State lock not released (Terraform process killed): run
terraform force-unlock <LOCK_ID>after confirming no other apply is running.
What not to do:
terraform destroyand start over — this deletes the resources that were created successfully, including any data they hold.- Edit the
.tffiles to remove the failed resource — this makes Terraform think the successfully created resources should also be destroyed.
Key Points
- Terraform writes to state after each resource, so partial progress is preserved
- Run
terraform planafter a failure — it shows only the remaining work - Fix the root cause (permissions, limits, arguments) before retrying
- Never destroy-and-recreate or remove resources from config to work around a partial failure
Explanation
What interviewers look for
Look for understanding that state captures partial progress. Weak answers suggest destroying everything and starting over. Strong answers explain the incremental state model and mention edge cases like orphaned resources or stuck locks.
More Terraform questions
- When do you need depends_on in Terraform, and when is it a code smell?
- What is Terraform, and how does it work at a high level?
- How do you configure OIDC authentication between GitHub Actions and AWS for Terraform pipelines?
- How do you use Terraform to deploy the same infrastructure to nonprod and production AWS accounts?
- What are the main terraform state subcommands, and when do you use them?
- Someone manually deletes an EC2 instance that Terraform manages. What happens on the next plan and apply?
- How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
- Walk through terraform init, plan and apply. What does each do?