Open mivok opened 3 years ago
Thanks for filing the issue @mivok, you can see some more details on a similar issue in #25836. I'll leave this one open for now, since it has the same root cause but arrives there via a slightly different use case.
Is there a workaround for this problem, @jbardin? Finding the right workspace structure to allow targeted applies for divergent branches is hard, and modules containing linked resources seemed like it would be sufficient. Clearly not a well-supported use-case, which is probably fair, but sometimes still necessary.
@skeggse a workaround would depend on the specific use case. Some situations are not really solvable in the general sense, because the targeting can create unknown values which require the changes to be evaluated during apply. Since -target
is only intended to be a temporary workaround for problems when a complete config cannot be applied, the shortcomings appear differently depending on the problem at hand. The only advice I could offer other than restructuring the config to be applied as a whole, would be to try and use multiple applies with more narrowly defined-target
values.
Interestingly, from recent experience, the plan does not match the apply. If I apply a targeted plan such as those described in this thread, only the -target
ed resources get applied, and some subset of the plan gets ignored. In other words, the "Plan: 1 to add, 0 to change, 1 to destroy." does not match the final counts.
Terraform Version
I've also observed this on 0.14.6
Terraform Configuration Files
main.tf:
foo/main.tf:
Debug Output
TF_LOG=trace terraform apply -target='module.foo["baz"]' -no-color 2>&1 | tee debug.txt
``` 2021-07-07T17:34:18.412-0400 [DEBUG] Adding temp file log sink: /var/folders/t0/fdl29c0s73g97g_rrff8m0v80000gp/T/terraform-log651780678 2021-07-07T17:34:18.412-0400 [INFO] Terraform version: 1.0.2 2021-07-07T17:34:18.412-0400 [INFO] Go runtime version: go1.16.4 2021-07-07T17:34:18.412-0400 [INFO] CLI args: []string{"/usr/local/Cellar/tfenv/2.1.0/versions/1.0.2/terraform", "apply", "-target=module.foo[\"baz\"]", "-no-color"} 2021-07-07T17:34:18.412-0400 [TRACE] Stdout is not a terminal 2021-07-07T17:34:18.412-0400 [TRACE] Stderr is not a terminal 2021-07-07T17:34:18.412-0400 [TRACE] Stdin is a terminal 2021-07-07T17:34:18.412-0400 [DEBUG] Attempting to open CLI config file: /Users/markharrison/.terraformrc 2021-07-07T17:34:18.412-0400 [INFO] Loading CLI configuration from /Users/markharrison/.terraformrc 2021-07-07T17:34:18.413-0400 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins 2021-07-07T17:34:18.413-0400 [DEBUG] ignoring non-existing provider search directory /Users/markharrison/.terraform.d/plugins 2021-07-07T17:34:18.413-0400 [DEBUG] ignoring non-existing provider search directory /Users/markharrison/Library/Application Support/io.terraform/plugins 2021-07-07T17:34:18.413-0400 [DEBUG] ignoring non-existing provider search directory /Library/Application Support/io.terraform/plugins 2021-07-07T17:34:18.413-0400 [INFO] CLI command args: []string{"apply", "-target=module.foo[\"baz\"]", "-no-color"} 2021-07-07T17:34:18.414-0400 [TRACE] Meta.Backend: no config given or present on disk, so returning nil config 2021-07-07T17:34:18.414-0400 [TRACE] Meta.Backend: backend has not previously been initialized in this working directory 2021-07-07T17:34:18.414-0400 [DEBUG] New state was assigned lineage "9b42fa73-b588-ff24-3c30-a860324f38b5" 2021-07-07T17:34:18.414-0400 [TRACE] Meta.Backend: using default local state only (no backend configuration, and no existing initialized backend) 2021-07-07T17:34:18.414-0400 [TRACE] Meta.Backend: instantiated backend of typeSteps to Reproduce
terraform init
andterraform apply
main.tf
and changebar
tobaz
terraform plan
- terraform wants to createbaz.txt
and deletebar.txt
. This is expected.terraform plan -target 'module.foo["baz"]'
. Terraform still wants to deletebar.txt
as well as creatingbaz.txt
.Expected Behavior
module.foo["baz"]
, terraform should only want to create baz.txtActual Behavior
Additional Context
This is a minimal example, using
local_file
and a simple text file.It only appears to happen when using for_each on a module. If I do the same thing, but with the for_each directly on a local_file resource, terraform correctly allows targeting just a specific instance of the resource.