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]"
}
}
]
}
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