Skip to content
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

Interpolation issue for multi-dimensonal/nested lists #11036

Closed
vancluever opened this issue Jan 4, 2017 · 7 comments
Closed

Interpolation issue for multi-dimensonal/nested lists #11036

vancluever opened this issue Jan 4, 2017 · 7 comments
Milestone

Comments

@vancluever
Copy link
Contributor

Possibly HIL/HCL related, might be known, couldn't find an issue in my <5min sanity check. Logging to track.

EDIT: Related: #8047

The Variable

variable "public_subnet_addresses" {
  type = "map"

  default = {
    production = ["10.0.0.0/25", "10.0.0.128/25"]
    staging    = ["10.0.1.0/25", "10.0.1.128/25"]
  }
}

Good

output "element_from_list_from_map_func" {
  value = "${element(var.public_subnet_addresses["production"], 0)}"
}               

Gives:

element_from_list_from_map_func = 10.0.0.0/25

Bad

output "element_from_list_from_map" {
  value = "${var.public_subnet_addresses["production"][0]}"
}

Gives:

Error loading config: Error loading variables.tf: Error reading config for output element_from_list_from_map: parse error at 1:48: expected "}" but found "["
@apparentlymart
Copy link
Contributor

apparentlymart commented Jan 4, 2017

Hi @vancluever,

You're right that this is a HIL issue rather than directly a Terraform issue, and indeed I've been working on it over in hashicorp/hil#42 though I stalled a bit over the holidays.

I don't remember there being a Terraform issue about this yet, so I'm going to leave this here to represent the need to update the vendored HIL and make the required changes to Terraform's interpolation functions once that issue is resolved.

@jlsjonas
Copy link

The same issue (but, as far as I could see, no workaround) occurs when trying to do the invers (having a map inside a list)

example:

locals {
  test = [{
    a = "a1"
    b = "b1"
  },{
    a= "a2"
    b = "b2"
  }]
}

There's no way to actually get the vars, only the entire object

(one of my attempts, errors out)

//output "test" {
//  value = "${element(local.test[0], "timeSpan")}"
//}
``

@sadpirit
Copy link

locals { test = [{ a = "a1" b = "b1" },{ a= "a2" b = "b2" }] }

Try:
${lookup(local.test[0], "a")}

@joariasl
Copy link

joariasl commented Feb 20, 2018

For nested maps, for example, for configure it based on the Terraform workspace, you can use:

variable "nested_map_name" {
  default = {
    default    = {
      value1 = 10
      value2 = false
    }
    staging    = {
      value1 = 20
      value2 = false
    }
    production = {
      value1 = 50
      value2 = true
    }
  }
}

resource "resource_where_use_nested_map" "resource_name" {
  resouce_param1 = "${lookup(var.nested_map_name[terraform.workspace], "value1")}"
  resouce_param2 = "${lookup(var.nested_map_name[terraform.workspace], "value2")}"
}

(Remember that you don't have to add interpolation on Terraform variables)

@apparentlymart
Copy link
Contributor

I've just tried a variant of the original non-working example on Terraform v0.12.0-alpha1:

variable "public_subnet_addresses" {
  type = "map"

  default = {
    production = ["10.0.0.0/25", "10.0.0.128/25"]
    staging    = ["10.0.1.0/25", "10.0.1.128/25"]
  }
}

output "element_from_list_from_map" {
  value = var.public_subnet_addresses["production"][0]
}

And now it works as expected!

$ terraform apply

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

Outputs:

element_from_list_from_map = 10.0.0.0/25

In the end this has been resolved not by a change in HIL but by moving Terraform over to HCL 2.0, which incorporates HIL-like expression functionality but has full support for chaining attribute and index syntax after any expression.

Thanks for reporting this!

@ozbillwang
Copy link

ozbillwang commented May 16, 2019

this blog is helpful, it gives full solution with real example you can reference.

https://salekseev.com/posts/nested-maps-in-terraform-variables/

@ghost
Copy link

ghost commented Jul 25, 2019

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.

@ghost ghost locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants