Hi, i'm Lucian and here I share my experiences, thoughts and opinions on life in the blue cloud. I'm a Cloud Solution Architect, specialising in Azure infrastructure, at Microsoft, in Sydney, Australia.

What's wrong with removing a RouteTable association with AzureAz Powershell 🤦

Background

For a change recently, I needed to disassociate Azure RouteTable’s from subnets, specifically: I needed to this at scale. It wasn’t a matter of a couple of RouteTable’s. Rather, the design had close to a RouteTable per subnet (with many subnets across many VNETs). The environment is also spread across multiple logical zone types and VNETs are also spread across multiple subscriptions. Unfortunately using a handful of RouteTable’s assigned to a large spread of subnets just isn’t possible.

When working with the Azure Az PowerShell module, the documentation outlines the following cmdlet to associate a RouteTable to a subnet:

Set-AzVirtualNetworkSubnetConfig `
-VirtualNetwork $virtualNetwork `
-Name 'Public' `
-AddressPrefix 10.0.0.0/24 `
-RouteTable $routeTablePublic | `
Set-AzVirtualNetwork

Note: that you’ll need to grab the $virtualNetwork and $routeTablePublic first.

The reference for this can be found in the following docs.microsoft.com article.

The problem for me though is that the examples don’t highlight how to disassociate a RouteTable from a subnet. Looking back over any reference material I had, I managed to dig out an AzureRM PS cmdlet that did just that. Here’s that RM PS example:

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName vnetResourceGroupName -Name virtualNetworkName
Set-AzureRmVirtualNetworkSubnetConfig -Name subnetName `
-VirtualNetwork $vnet `
-AddressPrefix <10.0.0.0/24> `
-RouteTable $null | `
Set-AzureRmVirtualNetwork

I took that and appropriated it for use with AzureAz PS. Here’s the example of what I was trying to execute:

$VNET = Get-AzVirtualNetwork -Name -ResourceGroupName
Set-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNET `
-Name subnet `
-AddressPrefix <10.0.0.0/24> `
-RouteTable $null | `
Set-AzVirtualNetwork

The result of this was that the cmdlet I was executing was not actually working. It would certainly execute, with no errors either mind you, but the JSON output on screen was an unchanged output, showing the following under RouteTable configuration:

"RouteTable": {
"DisableBgpRoutePropagation": false,
"Id": "/subscriptions/<GUID>/resourceGroups/<resourceGroup>/providers/Microsoft.Network/routeTables/<routeTable>"
},

Solution

Rather than raising a Microsoft Premier support case, I thought I’d raise an Issue via Github to the AzureAz PowerShell development team. In no time at all, the solution was found. Thanks to Anton Evseev who was able to provide some insight.

The cmdlet Set-AzVirtualNetworkSubnetConfig seems to only update the RouteTable configuration if the RouteTable assignment is not $null. Anton referenced the module details where on line 58 he’s showing that via if (this.RouteTable != null). This is actually pretty interesting. I didn’t realise that this level of detail was available on Github (well, to be honest, I never thought to look). So thankfully, not only did Anton provide feedback on how to actually disassociate a RouteTable from a subnet; he also shared a means for me (and anyone) to look into modules to see what they’re doing and why. In future, raising that Issue might not be needed?!?!

How to disassociate a RouteTable from a subnet via AzureAz PowerShell:

$vnet = Get-AzVirtualNetwork -Name virtualNetwork -ResourceGroupName resourceGroup
$subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name subnet
$subnet.RouteTable = $null
Set-AzVirtualNetwork -VirtualNetwork $vnet

Enjoy 😄