Skip to content

count and for_each for modules #17519

@AnthonyWC

Description

@AnthonyWC

Is it possible to dynamically select map variable, e.g?

Currently I am doing this:

vars.tf

locals {
  map1 = {
    name1 = "foo"
    name2 = "bar"
  }
}

main.tf

module "x1" {
  source = "../"
  parameter = "${local.map1["name1"]}"
}

module "x2" {
  source = "../"
  parameter = "${local.map1["name2"]}"
}

This works but it's repetitive/DRY to hardcode the key name.

Ideally I want to able to do something like this:

module "x1" {
  source = "../"
  parameter = "${local.map1[$var.select]}"
}

Where I can dynamically alter the key variable in the same file. I thought about using null_data_source:

data "null_data_source" "test" {
  inputs = {
    current = "${var.selector}"
  }
}

parameter  = "${local.map1[data.null_data_source.test.outputs["current"]]}"

But don't think I will able to inject different variable to selector value since I can't use locals within module block.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions