Terraform: Nested lists are flattened and can't be accessed using index syntax

Created on 9 Oct 2018  ยท  5Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.11.8

Terraform Configuration Files

locals {
  list = [
    ["0", "1"],
    ["2", "3"],
  ]
}

variable index {
  default = 1
}

output item {
  value = "${local.list[var.index]}"
}

Expected Behavior

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.

Actual Behavior

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

item = 1
bug config

Most helpful comment

No problem, thanks for the explanation. Currently, I use maps as a workaround:

locals {
  map= {
   "0" = ["0", "1"],
   "1" = ["2", "3"],
  }
}

All 5 comments

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:

  • cosmos_db_that_might_not_exist is a resource we create only in some environments hence the splat operator, which causes the outer list. Connection_strings is a list of all connection strings of the Cosmos DB.
  • concat with list of list with 1 string so that there is at least one element in the outer list and nested list (therefor providing fallback to empty string).
  • in the output there is combination of direct array accessing (retrieving first element from outer list) and "element' function for retrieving the result from inner list. This combination is required, otherwise the module loading fails.
  • It must be split to the two parts - into local variable & output, otherwise the module loading complains about 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franklinwise picture franklinwise  ยท  3Comments

ronnix picture ronnix  ยท  3Comments

darron picture darron  ยท  3Comments

carl-youngblood picture carl-youngblood  ยท  3Comments

zeninfinity picture zeninfinity  ยท  3Comments