The problem happens where:
- compatibility is required with both Terraform 0.12.x and 0.13.x
data {}
block requires an attribute from aresource {}
block, like this:
resource google_compute_network initial {
name = "example-vpc"
auto_create_subnetworks = false
}
data google_compute_network passthrough {
name = google_compute_network.initial.name
# tf-0.12: Do not use depends_on!
# tf-0.13: Needs depends_on for this particular case.
# tf-0.14: Works well both ways.
depends_on = [google_compute_network.initial]
}
resource google_compute_subnetwork this {
name = "example-subnet"
network = data.google_compute_network.passthrough.self_link
ip_cidr_range = "192.168.55.0/24"
}
The behavior of the example is:
-
For Terraform-0.12, the consequences are quite dire and need to be documented in the corresponding README document:
- Never run
terraform refresh
, it forces a subsequent destroy/recreate of multiple resources. Recover from it by restoring the tfstate from backup. No workaround available, which means that the production environment can never be refreshed to fully re-read the currently existing cloud resources. - Every
apply
will also destroy/recreate all the dependant resources. - The workaround is to always use:
terraform apply --refresh=false
- For planning, use:
terraform plan --refresh=false
- Actually tested on 0.12.29.
- Never run
-
For Terraform-0.13 (actually tested 0.13.6), all works well.
-
For Terraform-0.14+, all is good both ways.
You can simply remove the depends_on
above.
-
For Terraform-0.12 (actually tested 0.12.29), all works well.
-
For Terraform-0.13:
- The
terraform apply
just fails (except when the resource exists already). - The
terraform apply --refresh=false
fails in an identical manner. - Only some of the types of
data
objects are impacted, not sure which (e.g. it happens withdata.google_compute_network
but not withdata.google_compute_subnetwork
). - The workaround is:
terraform apply --target google_compute_network.initial
- The workaround is user-visible and needs to be documented in the corresponding README document.
- Actually tested on 0.13.6.
- The
-
For Terraform-0.14+, all is good both ways.
Unsuccessful attempt at explaining the root cause: this support thread
for 0.13 suggests that the behavior exists because the attribute is marked non-computed, however in our case the attribute
data.google_compute_network.passthrough.self_link
is seemingly marked computed per the
source code.
Thus, the alternative recommendations are (sorted starting from the best one):
-
Strong recommendation to require the latest GA version of Terraform enforcing it with
required_version
mechanism. -
Test the code with Terraform 0.13.x (a
data {}
block requiring an attribute coming from aresource {}
block), if impacted:- avoid this code pattern altogether,
- alternatively, keep
depends_on
but drop the 0.12.x compatibility withrequired_version = ">= 0.13"
- alternatively, remove
depends_on
from code and accept that 0.13 will not run cleanly; document the precise workaround commands for users.