Terraform interview question · Most asked
What does terraform plan -target do, and why is it risky?
Definition Asked in DevOps Engineer · Cloud Engineer · Site Reliability Engineer (SRE) interviews
Summary
-target tells Terraform to plan and apply only the named resource (and its
dependencies). It is meant for exceptional recovery situations, not normal workflow,
because it leaves state partially updated and can cause drift between what Terraform
knows and what exists.
Longer Answer
When you run terraform plan -target=aws_instance.web, Terraform:
- Refreshes only the targeted resource and its upstream dependencies.
- Computes a plan that ignores everything else in the configuration.
- On apply, writes state only for the resources it touched.
Legitimate uses:
- Unblocking a stuck apply: one resource keeps failing and blocks fifty others. Target the failing resource to fix it, then run a full plan to reconcile.
- Bootstrapping circular dependencies: rare cases where resource A needs to exist before resource B can be planned, but both are in the same configuration.
- Emergency hotfix: production is down and you need to change one security group rule without waiting for a full plan across hundreds of resources.
Why it's risky:
- Partial state: other resources may now be out of sync. A subsequent full plan may show unexpected changes because Terraform didn't refresh everything.
- Skipped validation: resources that depend on the target but aren't targeted themselves are not validated — they may fail on the next full apply.
- Habit formation: teams that use
-targetroutinely are usually avoiding slow plans, which is a symptom of needing to split the state, not target individual resources.
Terraform itself prints a warning: "Resource targeting is in effect" and "This is not
a normal workflow." Always follow a targeted apply with a full terraform plan to
verify consistency.
Key Points
-targetplans only the named resource and its dependencies, ignoring everything else- Leaves state partially updated — always follow with a full plan to reconcile
- Legitimate for emergency fixes and unblocking stuck applies, not for routine use
- Routine
-targetuse usually means the state needs to be split, not targeted
Explanation
What interviewers look for
Look for awareness that -target is a recovery tool, not a workflow optimisation. Weak answers describe it as a way to speed up applies. Strong answers mention the partial state risk and the need for a reconciliation plan afterwards.
More Terraform questions
- When do you need depends_on in Terraform, and when is it a code smell?
- Someone manually deletes an EC2 instance that Terraform manages. What happens on the next plan and apply?
- 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?
- Terraform apply fails halfway through — some resources were created, others were not. How do you recover?
- How should nonprod and production Terraform configurations differ, and how do you enforce those differences?
- What is Terraform, and how does it work at a high level?