krishna m
krishna m

Reputation: 247

Use a list/tuple in a terraform function using for_each

i have below output from terraform locals .

  [
    [
      "rt1",
      "rg1",
      "10.0.0.0.1/26",
    ],
    [
      "rt1",
      "rg1",
      "10.0.0.32/24",
     ],
     [
      "rt2",
      "rg2",
      "10.0.0.0.1/26",
     ],
     [
      "rt2",
      "rg2",
      "10.0.0.32/24",
     ],
     [
      "rt3",
      "rg3",
      "10.0.0.0.1/26",
     ],
     [
      "rt3",
      "rg3",
      "10.0.0.32/24",
     ],
   ]

Below is the function. As the output values are 6 sets/tuples .. it should loop through 6 times and replace rt,rg and ip in routetable,resourcegroup and subnet cidr. I am able to get the output through count .. But i want to do this with for_each

resource "azurerm_route" "route" {
 count     = length(local.flattened)
 name                = "test"
 resource_group_name = ((local.flattened[count.index])[1])
 route_table_name    = ((local.flattened[count.index])[0])
 address_prefix      = ((local.flattened[count.index])[2])
 next_hop_type       = "VirtualAppliance"
 next_hop_in_ip_address = "10.220.54.16"            
 }

Upvotes: 1

Views: 12228

Answers (1)

Marcin
Marcin

Reputation: 238081

I think the following should work:

resource "azurerm_route" "route" {
   for_each            = {for i,v in local.flattened: i=>v}
   name                = "test"
   resource_group_name = each.value[0]
   route_table_name    = each.value[1]
   address_prefix      = each.value[2]
   next_hop_type       = "VirtualAppliance"
   next_hop_in_ip_address = "10.220.54.16"            
 }

Upvotes: 4

Related Questions