How to reference item in map object (Terraform 0.11.14)

Terraform version 0.11.14

I am constrained to that version and not permitted to upgrade

I can’t seem to be able to reference the item name within my below variable. Is this possible in terraform 0.11.14

locals {
  workspace = "feature"
}
variable "cool_variable" {
  description = "cool description"
  type        = "map"
  default = {

  names = [
    { name = ["jame"], description = "description" },
    { name = ["liz"], description = "another description" },
    { name = ["cool_name"], description = "Ps" },
    { name = ["mora"], description = "roxy" },
    { name = ["jane"], description = "ster 1" },
  ],
    other_names = [
    { name = ["jame"], description = "description" },
    { name = ["liz"], description = "another description" },
    { name = ["cool_name"], description = "Ps" },
    { name = ["mora"], description = "roxy" },
    { name = ["jane"], description = "ster 1" },
  ]
  }

}

output "test" {
  value = "${var.cool_variable[local.workspace][0]["name"]}"
}

Error message

Error: Error loading terratest/main.tf: Error reading config for output test: parse error at 1:37: expected “}” but found “[”

Oh :frowning:

I think that wouldn’t work because of the nested quotes?

I’d write it as

  value = "${var.cool_variable[local.workspace][0].name}"

I have tried the above as well same error, it seems as though anything after

${var.cool_variable[local.workspace]

causes it to throw an error

e.g.

value = "${var.cool_variable[local.workspace].*}"

error

Error reading config for output test: parse error at 1:37: expected “}” but found “.”

Sorry, Terraform 0.11 is so old that the only thing I ever did with it was migrate other people’s old configs to later versions.

1 Like

Hi @david-oyeku,

Unfortunately the data structure you want to represent here is not supported by Terraform v0.11 and earlier, because those versions don’t yet have support for object types, which would be required for you to have an mapping where one attribute is a list and the other attribute is a string.

Unfortunately I think what you intend to achieve will not be possible in Terraform v0.11. You will need to find a simpler alternative design that can be represented within Terraform v0.11’s far more limited type system, which only supports:

  • strings (type = "string" in v0.11, type = string in modern Terraform)
  • lists of strings (type = "list" in v0.11, type = list(string) in modern Terraform)
  • maps of strings (type = "map" in v0.11, type = map(string) in modern Terraform)

To access the elements of a list of strings you can write var.example.0.

To access the elements of a map of strings you can write var.example.key.

Deeply-nested data structures don’t work properly in Terraform v0.11, which is a big part of why Terraform v0.12 completely replaced the type system.

1 Like