How to add existing route table to subnet

Viewed 27

I am new to bicep and I am trying to attach an existing UDR named "udr-test" to 2 subnets, unfortunately with no luck.

This is my template that work fine but without UDR configuration:

var addressSpace = [
  '10.0.0.0/16'
]

var subnets = [
  {
    name: 'Subnet1'
    subnetPrefix: '10.0.2.0/24'
  }
  {
    name: 'Subnet2'
    subnetPrefix: '10.0.3.0/24'
  }
]

resource VNET 'Microsoft.Network/virtualNetworks@2021-02-01' existing  = {
  name: 'vnet-test'
}

@batchSize(1)
resource Subnets 'Microsoft.Network/virtualNetworks/subnets@2020-11-01' = [for (sn, index) in subnets: {
  name: sn.name
  parent: VNET
  properties: {
    addressPrefix: sn.subnetPrefix
  }
}]

Do you know how I can modify the template to add the UDR as well?

1 Answers

There is routeTable property on the subnet resource (see documentation):

// reference to existing route table 
resource routeTable 'Microsoft.Network/routeTables@2022-01-01' existing  = {
  name: 'udr-test'
}

@batchSize(1)
resource Subnets 'Microsoft.Network/virtualNetworks/subnets@2020-11-01' = [for (sn, index) in subnets: {
  name: sn.name
  parent: VNET
  properties: {
    addressPrefix: sn.subnetPrefix
    routeTable:{
      id: routeTable.id // assign the route table
    }
  }
}]
Related