0

I have a route_table variable containing route table objects which each have several route objects.

variable "route_tables" {
  description = "a map of route tables"
  type        = map(any)

  default = {
    "route_table01" = {
      name = "route_table01"
      route = {
        name                   = "route1"
        address_prefix         = "17.65.255.0/24"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.18.128.6"
      }
      route = {
        name                   = "route2"
        address_prefix         = "204.61.3.87/32"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.18.128.6"
      }
      route = {
        name                   = "route3"
        address_prefix         = "13.61.37.248/29"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.18.128.6"
      }
    }
  }
}

I'm now trying to loop over them in my module:

resource "azurerm_route_table" "route_tables" {
  for_each = var.route_tables

  name                = each.value.name
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name
  tags                = local.tags
  dynamic "route" {
    for_each = each.value.route
    content {
      name                   = each.value.route.name
      address_prefix         = each.value.route.address_prefix
      next_hop_type          = each.value.route.next_hop_type
      next_hop_in_ip_address = each.value.route.next_hop_in_ip_address
    }
  }
}

The tf plan completes successfully, but only 1 route is being added to the route table:

enter image description here

How can I loop over these routes so that all of them are added to the route table?

1
  • 3
    You have the same key three times. Commented Jan 11, 2023 at 21:21

1 Answer 1

1

I believe one change you'll want to make is that you currently have nothing allowing you to iterate through each nested block. To use for_each, your dynamic block needs to use a different collection as the basis for how it iterates. Instead:

resource "azurerm_route_table" "route_tables" {
  for_each = {
    for routes in var.route_tables : routes.name => {
     name                = routes.name
     location            = routes.location
     resource_group_name = routes.resource_group.name
     route               = routes.route
   }
 }    
 name                = each.value.name
 location            = each.value.location
 resource_group_name = each.value.resource_group_name
 dynamic "route" {
    for_each = each.value.route
    content {
      name                   = route.value.name
      address_prefix         = route.value.address_prefix
      next_hop_type          = route.value.next_hop_type
      next_hop_in_ip_address = route.value.next_hop_in_ip_address
    }
  }
}

Where your variable might be

variable "route_tables" {
  description = "List of route tables to create."
  type        = any
  default     = []
}

And your input might be

  route_tables = [
    {
      name                = ""
      location            = ""
      resource_group_name = ""
      num_loops   = 2
      route = [
        {
          name                   = ""
          address_prefix         = ""
          next_hop_type          = ""
          next_hop_in_ip_address = ""
        },
        {
          name                   = ""
          address_prefix         = ""
          next_hop_type          = ""
          next_hop_in_ip_address = ""
        }
      }
    ]
  }

Good luck and I hope this helps.

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.