Saturday 19 November 2022

Resolve "BadRequest message: error: code: InUseSubnetCannotBeDeleted" with Bicep IaC

 Bicep is really simple and easy to implement infrastructure as Code tool for Azure. However, there is a bit of an issue with subnet resource when we try to redeploy. Even without no changes Bicep is trying to delete and create subnet resources and cmplaining a subnet cannot be deleted, because the resources are using it. Let's try to understand the problem and a solution to fix the issue.

The Scenario

Let's take a simple scenario where we want to deploy below resources.

  • Resource Group
  • Virtual Network (vNet)
  • Subnet
  • Network Interface in the subnet

We can create a network.bicep as shown below.

param vnetName string
param snetName string
param networkInterfaceName string
param location string

resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' = {
  name: vnetName
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
  }
}

resource subnet 'Microsoft.Network/virtualNetworks/subnets@2021-05-01' = {
  name: snetName
  parent: vnet

  properties:{
    addressPrefix:'10.0.0.0/24'
    privateEndpointNetworkPolicies:'Enabled'
    privateLinkServiceNetworkPolicies:'Enabled'
  }
}

resource nic 'Microsoft.Network/networkInterfaces@2021-05-01' = {
  name: networkInterfaceName
  location: location
  properties: {
    ipConfigurations: [
      {
        name: 'ipconfig1'
        properties: {
          subnet: {
            id: subnet.id
          }
          privateIPAllocationMethod: 'Static'
          privateIPAddressVersion:'IPv4'
          privateIPAddress:'10.0.0.4'
        }
      }
    ]
  }
}

Then in our main.bicpe we can use the network.bicep module.

targetScope='subscription'
param rgName string = 'rg-snettest-01'
param location string = 'eastus'

param vnetName string = 'vnet-snettest-01'
param snetName string = 'snet-snettest-01'
param networkInterfaceName string = 'nic-snettest-01'

resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: rgName
  location: location
}

module vnet 'network.bicep' = {
  scope: rg
  name: 'vnet'
  params:{
    vnetName:vnetName
    snetName:snetName
    networkInterfaceName:networkInterfaceName
    location:location
  }
}

The Issue

Once we deploy the resources they deploys fine for the first time. But if we try to redeploy same bicep code above without any changes, the below error occurs.


The Fix

To fix the issue we can modify the subnet creation as shown below. We have to create the subnet with vnet resource and refer to it as an existing resource to use it in network interface.

param vnetName string
param snetName string
param networkInterfaceName string
param location string

resource vnet 'Microsoft.Network/virtualNetworks@2021-05-01' = {
  name: vnetName
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets:[
      {
        name:snetName
        properties:{
          addressPrefix:'10.0.0.0/24'
          privateEndpointNetworkPolicies:'Enabled'
          privateLinkServiceNetworkPolicies:'Enabled'
        }
      }
    ]
  }
}

resource subnet 'Microsoft.Network/virtualNetworks/subnets@2021-05-01' existing = {
  name: snetName
  parent: vnet
}

resource nic 'Microsoft.Network/networkInterfaces@2021-05-01' = {
  name: networkInterfaceName
  location: location
  properties: {
    ipConfigurations: [
      {
        name: 'ipconfig1'
        properties: {
          subnet: {
            id: subnet.id
          }
          privateIPAllocationMethod: 'Static'
          privateIPAddressVersion:'IPv4'
          privateIPAddress:'10.0.0.4'
        }
      }
    ]
  }
}

No comments:

Popular Posts