Originally published on Lucian’s blog, lucian.blog. Follow Lucian on Twitter @LucianFrango.

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!

Category:
PowerShell
Tags:
, ,

Join the conversation! 1 Comment

  1. Thanks for reporting that! Just came across at the same time of looking at the same issue (ha! nice timing). On github, yeah, that’s the way to go when running into issues from AzureRM or AZ, same as for documentation issues (docs.microsoft.com). Opening a case will take longer and sometimes ends up on workarounds, not actual fixes, and the fix to be considered “on the roadmap” (without any ETA). It’s like jumping some gates ๐Ÿ™‚

    Now on checking the code, that is actually a good source for explaining your issue and how to go from command to the actual source file… just an idea for a post ๐Ÿ˜‰ (as not everyone is familiar with github). You could also include how to go back to a previous version (through history/diff), for those cases where there is a regression issue.

    Reply

Leave a Reply