Azure diaries: InUseSubnetCannotBeDeleted

When re-deploying a Bicep template that defines a vnet with subnets, I get the error “InUseSubnetCannotBeDeleted”.

It seems like ARM is trying to re-create the nested subnets, even though they haven’t changed.

The solution is to add a redundant reference to all subnets via the “existing” keyword.

resource websiteVnet 'Microsoft.Network/virtualNetworks@2021-02-01' = {
  name: 'vnet${sfx}001'
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [ vnet_address_space ]
    }
    subnets: [ {
        name: 'snet${sfx}001'
        properties: {
          addressPrefix: '...'
        }
      }
      {
        name: 'snet${sfx}002'
        properties: {
          addressPrefix: '...'
        }
      }
    ]
  }
}

resource snetRef1 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' existing = {
  name: 'snet${sfx}001'
  parent: websiteVnet
}

resource snetRef2 'Microsoft.Network/virtualNetworks/subnets@2021-02-01' existing = {
  name: 'snet${sfx}002'
  parent: websiteVnet
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.