Prashanth Sams
Prashanth Sams

Reputation: 21169

How to pass a dynamic value in a variable name on run-time?

Here is what I have:

locals {
  timeseries = "desktop"
}

dynamic "request" {
  for_each = var.query_"#{local.timeseries}"_timeseries
  content {
    q     = request.value.q
    type  = request.value.type
    style = request.value.style
  }
}

What I expect:

for_each = var.query_desktop_timeseries

Upvotes: 0

Views: 87

Answers (1)

mjgpy3
mjgpy3

Reputation: 8957

If I'm understanding your question correctly, you're trying to resolve a variable name via interpolation. In terraform, there's is no way to do this.

If you're looking to resolve to a particular list of values, based on the value of variables, you could do that using a map to, well, map from your value to the variables they resolve to.

For example you could have something like

locals {
  timeseries = "desktop"

  timeseries_lookup = {
    desktop = var.query_desktop_timeseries
    # Other mappings would go here
  }
}

This could then be used, very similarly to your desired use-case, like the following

  for_each = local.timeseries_lookup[local.timeseries]

Upvotes: 1

Related Questions