Skip to content

Files

Latest commit

Sep 12, 2021
1aee807 · Sep 12, 2021

History

History

traversing-nested-maps

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Sep 12, 2021
Sep 12, 2021
Sep 12, 2021

Traversing Nested Maps

Terraform used to use splat expressions for this stuff (and I wish they still worked), but now this is all done with for expressions.

Using your example code as a guide, it's pretty straightforward to select the structure of the map where the address_space lists are the first element down, like this:

> local.subscriptions.MySecondSubcription.VirtualNetworks
{
  "vNet1" = {
    "address_space" = [
      "10.20.0.0/24",
      "192.168.3.0/24",
    ]
  }
  "vNet2" = {
    "address_space" = [
      "10.30.0.0/24",
      "192.168.4.0/24",
    ]
  }
}

Now, using the for expression, we can cherry pick the address_space into a list by iterating over that level of the map like this:

> [ for net in local.subscriptions.MySecondSubcription.VirtualNetworks : net.address_space ]
[
  [
    "10.20.0.0/24",
    "192.168.3.0/24",
  ],
  [
    "10.30.0.0/24",
    "192.168.4.0/24",
  ],
]

If the subdivided lists aren't helpful, you can flatten() the output:

> flatten([ for net in local.subscriptions.MySecondSubcription.VirtualNetworks : net.address_space ])
[
  "10.20.0.0/24",
  "192.168.3.0/24",
  "10.30.0.0/24",
  "192.168.4.0/24",
]

Credits

Inspired by the question in this Reddit thread.