-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 doesn't allow for executing element(list, index) on empty lists. #11210
Comments
HIL (the interpolation language) doesn't lazy evaluate the branches of the if statement yet. We need to do that, and we have plans too. That is the issue here. Noting that for the future. :) |
Similar issue with no if:
Fails with: |
I believe this is fixed as part of #11704. |
Hit that too and ended up with the same workaround |
This workaround works with resource variable lists as well: ip_configuration {
...
public_ip_address_id = "${length(azurerm_public_ip.virtual_machine_public_ip.*.id) > 0 ? element(concat(azurerm_public_ip.virtual_machine_public_ip.*.id, list("")), count.index) : ""}"
} |
Thanks @PGrunwald. This workaround worked for me as well.
|
Hi. |
Shout out to Terraform! This tool allow us to programmatically build our infrastructure, which is conceptually amazing. I've learned only today a dozen hacks to circumvent language deficiencies! This is just another one to add to the list. Thanks Terraform!!! |
Hi, I am using element
I have 3 subnets and it works pretty fine.... Can I randomize the subnet_id line? So that subnet Ids for instances are not chosen serially? |
How can this be achieved when the output needs to be a list?
In doing the above, I always end up with the following in my plan:
...Which fails horribly. I've tried different permutations, such as using |
Hello
|
Workaround does also the trick also with splat operator
|
I found a way to use the Workaround to return an empty list from a condition:
Here This might be handy for @absolutejam |
Glad there's a workaround for this interpolation issue, but it really isn't ideal. This workaround for not having lazy evaluation breaks the wrap-around behavior of the element() function in the non-empty case, as there's now an incorrect dummy element at the end. That issue isn't keeping me from using this workaround, just pointing out that the currently accepted solution has limitations. Any update on where lazy eval for interpolation is on the roadmap? |
The conditional operator will only evaluate the selected expression in the next major release of Terraform. Based on the discussion here it seems like this issue is effectively the same as #15605, in spite of the different proximate cause, so I'm going to close this one just to consolidate discussion over there. Any further updates from the team will be posted on that issue. |
Excellent -- thanks for the heads up, @apparentlymart ! |
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. |
I've building a generic module where I optionally allocate the private_ip for the instance. Since terraform doesn't have the ability to optionally set a variable via an if block, I only want private_ip to have a non-null variable in certain cases. In most cases, the ip address is dynamically set by AWS, but on the rare occasion, we want to have it set to an IP (think DB instance), so I used a ternary.
My naive first implementation was as follows (note, we ensure that num_nodes == length(var.private_ips) when the module is being called).
This caused the following errors to be shown
After much hand-wringing, the following workaround was done, which seems very very hacky.
Basically, I force element to have a non-empty list by appending an extra blank element to the list (that will never be used) to keep the compiler/parse/runtime interpolator happy.
This works when there are the variable 'private_ips' is empty (the default), but still allows for setting the variable to a non-empty list and assign the ip appropriately, but the implementation feels so very wrong.
Regardless, I'm very happy that setting private_ip to a blank variable 'Does The Right Thing', even if takes some dancing around to make it work. :)
The text was updated successfully, but these errors were encountered: