Skip to main content
Resource targeting in Terraform lets you limit an operation (plan or apply) to specific resource(s) instead of evaluating the entire configuration. This is an advanced feature intended for exceptional situations such as recovery, debugging, or fixing drift. Misusing it can leave your infrastructure in an inconsistent or unsafe state, so read carefully. What resource targeting does:
  • Runs Terraform only against the targeted resource(s) and their dependency chain.
  • Is invoked with the -target flag on terraform plan or terraform apply.
  • Is not a performance optimization; it skips evaluating unrelated parts of the configuration and can therefore produce incomplete results.
Example command:
The image is an infographic titled "Resource Targeting," featuring three sections: "Targeting," "Recovery or Debugging," and "Skip Checks," each with a brief description of their purpose.
Call out the risk: if you skip evaluation of other configuration parts, ensure you are not skipping anything critical that could affect the overall deployment. When to use resource targeting Use resource targeting only in narrow scenarios where you understand the consequences. Common valid use cases include:
The image describes "Resource Targeting" with three options: Retry, Debug, and Drift, each with a brief explanation about when to use them.
Example: resource group, storage account, and container Below is a minimal example showing a resource group, a storage account that depends on the group, and a container that depends on the storage account.
Under normal operation Terraform evaluates the full dependency graph and creates or updates resources in the correct order. If you run:
Terraform will plan and apply only the container resource and any resources required to create it (in this example, the storage account and resource group if they do not already exist). This can be useful when the container creation previously failed and you want to retry only that resource. Why resource targeting is dangerous Resource targeting may skip planned changes to related resources such as account configuration, policy assignments, network rules, or security settings. Even if the targeted resource is created or updated successfully, the overall infrastructure might become inconsistent.
Resource targeting is intended for recovery, debugging, or drift-fix scenarios only. Do not use it for initial deployments, CI/CD pipelines, or regular day-to-day applies. If you care about correctness and safety, run a normal terraform apply (or terraform apply on a saved plan).
Practical scenario: retrying or fixing a single resource after a runtime error Case: you create a storage account, but the chosen account name is globally unique and already taken by another subscription. The plan phase can succeed, because Terraform does not detect the global uniqueness constraint; the failure occurs during apply when Azure rejects the creation. Example HCL that will fail during apply (name intentionally invalid for demonstration):
Typical workflow:
  1. Initialize:
  1. Create a plan:
  1. Apply the plan:
You might see an apply-time error like:
Fix the configuration by choosing a unique storage account name:
Then apply only the storage account (Terraform will also include any dependencies required to create it):
When you run the targeted apply, Terraform shows a concise plan covering just the targeted resource (and required dependencies). It will also display a warning that the plan was created with -target, for example:
Best practices and final rules
  • Use -target only for recovery, debugging, or drift correction—and only when you understand the implications.
  • Never use resource targeting for:
    • Initial provisioning
    • CI/CD pipelines
    • Regular operations as a performance shortcut
  • Prefer creating and applying a full plan (terraform plan + terraform apply) when correctness matters.
  • If you must target, document the action and follow up with a full plan/apply afterward to ensure consistency.
Additional references If you fully understand the impact and need to recover or debug, targeting can be a useful, narrowly scoped tool. Otherwise, rely on Terraform to manage the full dependency graph so your infrastructure remains consistent.

Watch Video