-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terraform only partially creates object in for loop #27966
Comments
Thanks for filing the issue @elliott-weston-cko! This one was quite the puzzle, since the behavior seemed to be indicating that the incorrect conditional was being chosen in some situations. That however was a red herring, as the underlying problem turned out to be the unification of What is actually going on is that when the conditional expression is attempting to evaluate the true and false arguments, a single result type must first be determined based in the arguments. In one case we have an object with the Any fix here would need to come from the upstream hcl or cty libraries, but in the meantime we can workaround the issue by explicitly declaring the types we want to operate on. In this case, wrapping all object literals in
|
Thank you for the prompt and in-depth explanation @jbardin 🙇🏽♂️ There is one slight nuance that is still puzzling me though I appreciate the terraform code is not the easiest to read, so I'm just going to walk through the thing that's puzzling me. name_prefix_formatting = var.name_prefixes == null ? null : [for n in var.name_prefixes : trimprefix(n, "/")]
name_prefix_stack_set = local.name_prefix_formatting == null ? local.initial_stack_set : flatten([for s in local.initial_stack_set : [ for n in local.name_prefix_formatting : { name = "${n}/${s.name}", branch = s.branch, subfolder = s.subfolder}]]) In these 2 lines we are doing some basic formatting, and then a null check. If the null check is true, use the values from In both scenarios, the final object that is assigned to the local {
name = <name>,
branch = <branch>,
subfolder = <subfolder>
} The only difference being there are more objects in the list if the null check is false. So when the I don't understand why in one scenario terraform makes the determination to drop the workspace attribute, and in the other scenario the determination is to keep the workspace attribute 🤔 Thanks again for the reply! |
Yes, this is really confusing. I only briefly mentioned it to avoid complicating this further, but there also appear to be bugs with the unification process for tuples and lists. While these two types will of course have slightly different requirements for determining what the resultant type can be, in this case the different code paths are also effecting how the types they contain are being unified. This is all part of the current investigation we are doing with the hcl and cty libraries. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Terraform Version
Terraform Configuration Files
main.tf
variables.tf
terraform.tfvars
Debug Output
https://gist.github.com/elliott-weston-cko/b898d186ade9e54949bc6f8d1be5225b
Expected Behavior
All the attributes defined in
local.workspace_stack_set
should be createdActual Behavior
The
workspace
attribute is missingSteps to Reproduce
terraform init
terraform plan
Additional Context
I'm trying to create the cartesian product of multiple variables, to do this I am chaining for-loops, and then finally creating a map with the name as the key (intention is for it to be used in a for_each loop).
If you perform the reproduction steps, you'll notice a few things.
The "final" set that is created has four attributes:
But in the output the workspace attribute is missing.
Initial thought was the for-loop that creates the workspace wasn't performing any iterations. However, the workspace is being evaluated as the key of the map contains the workspace attribute.
https://gist.github.com/elliott-weston-cko/9010a41a5fcb619b916c74f7cd54d4fb
To further confusion if the
name_prefixes
tfvars are commented out, so it has its default value of null, then the workspace attribute will appear in the output.https://gist.github.com/elliott-weston-cko/8f8e6941fbd4bbeb7b2f608032e3558a
References
The text was updated successfully, but these errors were encountered: