7

I have a fairly simple ARM template which I use to create vnet, subnets and service endpoints. When I try to change the service endpoints I get error "code": "InUseSubnetCannotBeDeleted". Stating that one of my VMs is using one of the subnets. However, I do not want to delete that subnet. I just want to update it, operation which I can do via portal or powershell just fine. Is there some switch I need to change to make the ARM template update resources and not create them from scratch?

Template. I stripped it down to bare minimum. First I use this to create vnet and two subnets, deploy one VM and then run the deployment again and I get the subnet cannot be deleted:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "type": "string",
      "defaultValue": "VNet1",
      "metadata": {
        "description": "VNet name"
      }
    },
    "vnetAddressPrefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/16",
      "metadata": {
        "description": "Address prefix"
      }
    },
    "subnets": {
      "type": "object"
    }
  },
  "variables": {
    "location": "[resourceGroup().location]",
    "subnetcount": "[length(parameters('subnets').settings)]"
  },
  "resources": [
    {
      "apiVersion": "2018-06-01",
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[parameters('vnetName')]",
      "location": "[variables('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": ["[parameters('vnetAddressPrefix')]"]
        }
      },
      "resources": [
      ]
    },
    {
      "apiVersion": "2018-06-01",
      "type": "Microsoft.Network/virtualNetworks/subnets",
      "name": "[concat(parameters('vnetName') , '/' , parameters('subnets').settings[copyIndex()].name)]",
      "location": "[variables('location')]",
      "copy": {
        "name": "subnetLoop",
        "count": "[variables('subnetcount')]"
      },
      "dependsOn": ["[parameters('vnetName')]"],
      "properties": {
        "addressPrefix": "[parameters('subnets').settings[copyIndex()].addressPrefix]"
      }
    }
  ]
}
Stringfellow
  • 2,788
  • 2
  • 21
  • 36
Kamsiinov
  • 1,315
  • 2
  • 20
  • 50

3 Answers3

18

I ran into the same issue. Here is what I have found, and it is basically the same answer as the other user above.

Three ways to create a vnet with subnets in an ARM Template. (very crude example)

1. Works the first time it runs. After that, the vnet resource tries to delete the subnet.

{
  "vnet"
},
{
  "subnet",
  "dependsOnVnet"
}

2. Even though they are nested resources, the vnet doesn't have contextual awareness. Similar to option #1.

{
  "vnet"
  resources : [
    {
      "subnet",
      "dependsOnVnet"
    }
  ]
}

3. The vnet resource is subnet aware because it is a property of the vnet. It will not delete the subnet.

{
  "vnet"
  "properties":{
    "subnets" : ["subnet"]
  }
}

*These examples are with an Incremental ARM Deployment.*

Community
  • 1
  • 1
Alex
  • 183
  • 1
  • 5
3

i suspect the problem is you left out something from the template and now its trying to delete it. its hard to tell what is wrong exactly here, but if you are trying to update existing subnet, you need to make sure all the subnets existing in the vnet are actually present in the template. if some subnets are leftout, it will try and delete them

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
0

Alex's answer #3 is the proper solution to this question. I want to add that changes to the subnets (like the name) will also trigger a delete/create. This is not apparent from the error message, but it easily seen if you run a what-if operation to see what Azure is changing (documentation).

bderusha
  • 11
  • 1