Overview
Terraform’s -target option is a powerful but high-risk mechanism intended only for exceptional use cases—typically for recovering from errors, partial failures, or when specifically instructed by Terraform itself. While targeting can help isolate actions on a specific resource, it can also cause unexpected behavior, especially when combined with explicit dependencies such as depends_on.
This article explains how targeting interacts with depends_on, why caution is required, and how to avoid unintentional resource creation/deletion.
Behavior of -target with depends_on
To target resources in HCP Terarform, we use TF_CLI_ARGS_plan environment variable with the value:
-target=resource.address.
If the targeted resource contains a depends_on meta-argument, Terraform must also include those dependencies to maintain a valid dependency graph.
Important Note
depends_on forces Terraform to treat the dependency as a required prerequisite, even if the configuration or resource type does not naturally require it.
Example Scenario
resource "random_pet" "name1" {
length = "15"
separator = "-"
depends_on = [ random_pet.name2 ]
}
resource "random_pet" "name2" {
length = "8"
separator = "-"
}
resource "random_pet" "name3" {
length = "8"
separator = "-"
}Let's say we try to target random_pet.name1 to be created.
Expected Behavior
The user expects only random_pet.name1 to be created.
Actual Behavior
Because random_pet.name1 has depends_on = [ random_pet.name2 ]
Terraform also includes random_pet.name2 in the plan, even though only random_pet.name1 was targeted.
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the
│ result of this plan may not represent all of the changes requested by the
│ current configuration.
│
│ The -target option is not for routine use, and is provided only for
│ exceptional situations such as recovering from errors or mistakes, or when
│ Terraform specifically suggests to use it as part of an error message.
╵
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# random_pet.name1 will be created
+ resource "random_pet" "name1" {
+ id = (known after apply)
+ length = 15
+ separator = "-"
}
# random_pet.name2 will be created
+ resource "random_pet" "name2" {
+ id = (known after apply)
+ length = 8
+ separator = "-"
}
Plan: 2 to add, 0 to change, 0 to destroy.Why This Happens
Terraform must maintain a valid dependency graph. When you explicitly specify a dependency—whether necessary or not—Terraform treats it as authoritative.
Therefore:
If A depends on B, targeting A forces Terraform to include B.
This applies to both creation and deletion.
This can result in much larger plans than intended.
Important Warning from Terraform Documentation
The official Terraform documentation warns that depends_on should be used sparingly because of its effects on planning:
“You should only use depends_on as a last resort because it can cause Terraform to create more conservative plans that replace more resources than necessary.”
Source: https://developer.hashicorp.com/terraform/language/meta-arguments/depends_on#processing-and-planning-consequences