Terraform v0.11.8
locals {
list = [
["0", "1"],
["2", "3"],
]
}
variable index {
default = 1
}
output item {
value = "${local.list[var.index]}"
}
This should return nested list in output: ["0", "1"] or ["2", "3"]
Terraform flattens nested lists, so using index doesn't allow one to access nested lists.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
item = 1
Hi @beatcracker!
I am sorry you've run across this problem. It is a known limitation of terraforms somewhat limited type system, and I believe this will behave the way you expect when terraform 0.12 is released later this year.
I will label this issue as a bug so we can circle back after the 0.12 release and validate.
No problem, thanks for the explanation. Currently, I use maps as a workaround:
locals {
map= {
"0" = ["0", "1"],
"1" = ["2", "3"],
}
}
Hi @beatcracker!
The changes that @mildwonkey mentioned are now merged to master and so I tried your configuration exactly as you wrote it in the v0.12.0-alpha2 prerelease build.
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
item = [
"2",
"3",
]
Success! :tada:
I think the problem you saw was a quirk of the language parser we used in versions 0.11 and prior. This has been significantly reworked for v0.12 and now has robust support for complex types like this.
The fix is already present in master and ready to be included in the forthcoming v0.12.0 release, so I'm going to close this out. Thanks for reporting this!
I just found a workaround for v0.11. It took me a whole day and it was by sheer luck when I was just about to give up.
In my case, I have a list of lists of connection strings that are retrieved from azurerm provider and all I need is the first one with fallback to the empty string. Also I'm sorry for too complex example, but I'm a bit afraid to try to simplify it as it would probably fall apart for one reason or another. Also I'm not willing to spend any more time on this.
locals {
list_of_lists_of_connection_strings = "${concat(azurerm_cosmosdb_account.cosmos_db_that_might_not_exist.*.connection_strings, list(list("")))}"
}
output "primary_connection_string" {
value = "${element(local.list_of_lists_of_connection_strings[0], 0)}"
}
To explain it:
expected ")" but found "["Please don't ask for more details - my PTSD is yet to be treated.
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.
Most helpful comment
No problem, thanks for the explanation. Currently, I use maps as a workaround: