Using the resource ID from a for_each resource block

Viewed 56

I've created a Terraform template that creates 2 route tables and 2 subnets using the for_each command. I am trying to associate the route tables to the two subnets, however I am struggling to do so because I don't know how to obtain the ID for the route tables and subnets as the details are not in a variable, and I'm not sure how to get that information and use it. Please may someone provide assistance?

Thank you

Main Template

# SUBNETS DEPLOYMENT
resource "azurerm_subnet" "subnets" {
  depends_on           = [azurerm_virtual_network.vnet]
  for_each             = var.subnets
  resource_group_name  = var.rg.name
  virtual_network_name = var.vnet.config.name
  name                 = each.value.subnet_name
  address_prefixes     = each.value.address_prefixes
}

# ROUTE TABLE DEPLOYMENT
resource "azurerm_route_table" "rt" {
  depends_on = [azurerm_virtual_network.vnet]
  for_each = var.rt
  name = each.value.route_table_name
  resource_group_name = var.rg.name
  location = var.rg.location
  disable_bgp_route_propagation = true
  route = [ {
    address_prefix = each.value.address_prefix
    name = each.value.route_name
    next_hop_in_ip_address = each.value.next_hop_ip
    next_hop_type = each.value.next_hop_type
  } ]
}

# ROUTE TABLE ASSOICATION
resource "azurerm_subnet_route_table_association" "rt_assoication" {
  subnet_id = azurerm_subnet.subnets.id
  route_table_id = azurerm_route_table.rt.id
}

Variables

# SUBNET VARIBALES
variable "subnets" {
  description = "subnet names and address prefixes"
  type = map(any)
  default = {
    subnet1 = {
      subnet_name      = "snet-001"
      address_prefixes = ["172.17.208.0/28"]
    }
    subnet2 = {
      subnet_name      = "snet-002"
      address_prefixes = ["172.17.208.32/27"]
    }
  }
}

# ROUTE TABLES VARIABLES
variable "rt" {
  description = "variable for route tables."
  type = map(any)
  default = {
    rt1 = {
      route_table_name = "rt1"
      address_prefix = "0.0.0.0/0"
      route_name = "udr-azure-firewall"
      next_hop_ip = "10.0.0.0"
      next_hop_type = "VirtualAppliance" 
    }
    rt2 = {
      route_table_name = "rt2"
      address_prefix = "0.0.0.0/0"
      route_name = "udr-azure-firewall"
      next_hop_ip = "10.0.0.0"
      next_hop_type = "VirtualAppliance" 
    }
  }
}

The error I get when I run terraform plan is:

│ Error: Missing resource instance key
│ 
│   on modules\vnet\main.tf line 74, in resource "azurerm_subnet_route_table_association" "rt_assoication":
│   74:   subnet_id = azurerm_subnet.subnets.id
│ 
│ Because azurerm_subnet.subnets has "for_each" set, its attributes must be accessed on specific instances.
│ 
│ For example, to correlate with indices of a referring resource, use:
│     azurerm_subnet.subnets[each.key]
╵
╷
│ Error: Missing resource instance key
│ 
│   on modules\vnet\main.tf line 75, in resource "azurerm_subnet_route_table_association" "rt_assoication":
│   75:   route_table_id = azurerm_route_table.rt.id
│ 
│ Because azurerm_route_table.rt has "for_each" set, its attributes must be accessed on specific instances.
│ 
│ For example, to correlate with indices of a referring resource, use:
│     azurerm_route_table.rt[each.key]
1 Answers

Looks like you are almost there, update the following in the subnet-route table association block, it should work:

# ROUTE TABLE ASSOICATION
resource "azurerm_subnet_route_table_association" "rt_assoication" {
  subnet_id = azurerm_subnet.subnets[each.key].id
  route_table_id = azurerm_route_table.rt[each.key].id
}
Related