Terraform: Is there any way to lookup a list in a map?

Created on 26 Sep 2018  ·  5Comments  ·  Source: hashicorp/terraform

Terraform latest version.

Is there an interpolation function (or combination of functions) which will take a list, and lookup each item in a map, to result in another list. e.g.

a_list = [ "one", "three", "four", "three" ]

a_map = {
    one = "1"
    two = "2"
    three = "3"
    four = "4"
}

SOME_FUNCTION(a_list, a_map) 
# [ "1", "3", "4", "3' ]

Thanks

question

Most helpful comment

Ok, consider me briefly nerd-sniped 🤣 But I got it 🎉

Code:

variable list {
  default = [ "one", "three", "four", "three"]
}

variable map {
  default = {
    one = "1"
    two = "2"
    three = "3"
    four = "4"
  }
}

data "null_data_source" "iterate_values" {
  count = "${length(var.list)}"
  inputs = {
    value = "${lookup(var.map, element(var.list, count.index))}"
  }
}

output iterate_values {
  value = "${data.null_data_source.iterate_values.*.outputs.value}"
}

Terraform:

$ tf apply
data.null_data_source.iterate_values[2]: Refreshing state...
data.null_data_source.iterate_values[3]: Refreshing state...
data.null_data_source.iterate_values[0]: Refreshing state...
data.null_data_source.iterate_values[1]: Refreshing state...

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

Outputs:

iterate_values = [
    1,
    3,
    4,
    3
]

All 5 comments

I'm not aware of a single interpolation function that does exactly what you describe. Can you share a bit more about the context where you would need this? Perhaps there's a way to simulate this behaviour.

I quicly cooked up the example below, which is close, but not exactly what you were looking for. I also experimented with a different approach which could potentially get you there, except that it uses count which doesn't work on outputs 😉

variable list {
  default = [ "one", "three", "four", "three"]
}

variable map {
  default = {
    one = "1"
    two = "2"
    three = "3"
    four = "4"
  }
}

output values {
  value = "${matchkeys(values(var.map), keys(var.map), var.list)}"
}

// ## Doesn't work since you can't use count on outputs
// output iterate_values {
//   count = "${length(var.list)}"
//   value = "${lookup(var.map, element(var.list, count.index))}"
// }

Ok, consider me briefly nerd-sniped 🤣 But I got it 🎉

Code:

variable list {
  default = [ "one", "three", "four", "three"]
}

variable map {
  default = {
    one = "1"
    two = "2"
    three = "3"
    four = "4"
  }
}

data "null_data_source" "iterate_values" {
  count = "${length(var.list)}"
  inputs = {
    value = "${lookup(var.map, element(var.list, count.index))}"
  }
}

output iterate_values {
  value = "${data.null_data_source.iterate_values.*.outputs.value}"
}

Terraform:

$ tf apply
data.null_data_source.iterate_values[2]: Refreshing state...
data.null_data_source.iterate_values[3]: Refreshing state...
data.null_data_source.iterate_values[0]: Refreshing state...
data.null_data_source.iterate_values[1]: Refreshing state...

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

Outputs:

iterate_values = [
    1,
    3,
    4,
    3
]

shiny 🎉 - Thanks a mil @bennycornelissen

@gtmtech you're welcome!

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