title: Nesting templates that deploy different subnets in the same vnet with AzureRM
body: <p>I am trying to achieve a level of containerization through templates. In the end, I would like to be able to define a component (one or more servers) that resides inside a virtual network and subnet (like a db cluster) in one template. Through another template, define a component that resides inside a virtual network and subnet, but possibly a different one.</p>
  
  <p>Template A:</p>
  
  <ul>
  <li>my-vnet
  
  <ul>
  <li>my-subnet1
  
  <ul>
  <li>my-interface1</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Template B:</p>
  
  <ul>
  <li>my-vnet
  
  <ul>
  <li>my-subnet2
  
  <ul>
  <li>my-interface2</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Such that, nesting Template B inside Template A would leave me with:</p>
  
  <p>Template AB:</p>
  
  <ul>
  <li>my-vnet
  
  <ul>
  <li>my-subnet1
  
  <ul>
  <li>my-interface1</li>
  </ul></li>
  <li>my-subnet2
  
  <ul>
  <li>my-interface2</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>Instead I get the following error:</p>
  
  <pre><code>New-AzureRmResourceGroupDeployment : 4:56:27 PM - Resource 
   Microsoft.Network/virtualNetworks 'overlayTest-vnet' failed with message '
  {
    "error": {
      "code": "InUseSubnetCannotBeDeleted",
      "message": "Subnet overlayTest-subnet2 is in use by  /subscriptions/beep-boop/resourceGroups/d/providers/Microsoft.Network/networkInterfaces/overlayTest-vnet-interface2/ipConfigurations/ipconfig1 and cannot be deleted.",
      "details": []
    }
  }
  </code></pre>
  
  <p>The resulting Resource Group is populated like this (as if the nested template deployed and the resources following the nesting quit).</p>
  
  <p>Template AB:</p>
  
  <ul>
  <li>my-vnet
  
  <ul>
  <li>my-subnet2
  
  <ul>
  <li>my-interface2</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>And my template is as follows:</p>
  
  <p>azuredeploy.json</p>
  
  <pre><code>{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "solutionName": {
        "type": "string",
        "defaultValue": "testing"
      },
      "vmIP": {
        "type": "string",
        "defaultValue": "10.0.100.100"
      },
      "virtualNetworkName": {
        "type": "string",
        "defaultValue": "[concat(parameters('solutionName'), '-vnet')]"
      },
      "vnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.0.0/16"
      },
      "subnetName": {
        "type": "string",
        "defaultValue": "[concat(parameters('solutionName'),'-subnet1')]"
      },
      "subnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.100.0/24"
      },
      "subnetName2": {
        "type": "string",
        "defaultValue": "[concat(parameters('solutionName'),'-subnet2')]"
      },
      "subnetAddressPrefix2": {
        "type": "string",
        "defaultValue": "10.0.101.0/24"
      },
      "_artifactsLocation": {
        "type": "string"
      },
      "_artifactsLocationSasToken": {
        "type": "securestring"
      }
    },
    "variables": {
      "OverlaySubnetTemplateFolder": "nestedtemplates",
      "OverlaySubnetTemplateFileName": "OverlaySubnet.json",
      "OverlaySubnetTemplateParametersFileName": "OverlaySubnet.parameters.json",
      "vnetID": "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
      "subnetRef": "[concat(variables('vnetID'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
      {
        "name": "OverlaySubnet",
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2016-09-01",
        "dependsOn": [],
        "properties": {
          "mode": "Incremental",
          "templateLink": {
            "uri": "[concat(parameters('_artifactsLocation'), '/', variables('OverlaySubnetTemplateFolder'), '/', variables('OverlaySubnetTemplateFileName'), parameters('_artifactsLocationSasToken'))]",
            "contentVersion": "1.0.0.0"
          },
          "parameters": {
            "solutionName": {
              "value": "[parameters('solutionName')]"
            }
          }
        }
      },
      {
        "apiVersion": "2015-06-15",
        "type": "Microsoft.Network/virtualNetworks",
        "name": "[parameters('virtualNetworkName')]",
        "location": "[resourceGroup().location]",
        "dependsOn": [],
        "properties": {
          "mode": "Incremental",
          "addressSpace": {
            "addressPrefixes": [
              "[parameters('vnetAddressPrefix')]"
            ]
          },
          "subnets": [
            {
              "name": "[parameters('subnetName')]",
              "properties": {
                "mode": "Incremental",
                "addressPrefix": "[parameters('subnetAddressPrefix')]"
              }
            }
          ],
          "virtualNetworkPeerings": []
        }
      },
      {
        "type": "Microsoft.Network/virtualNetworks/subnets",
        "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",
        "apiVersion": "2017-06-01",
        "properties": {
          "addressPrefix": "[parameters('subnetAddressPrefix')]",
          "privateAccessServices": []
        },
        "dependsOn": [
          "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]"
        ]
      },
      {
        "type": "Microsoft.Network/virtualNetworks/subnets",
        "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName2'))]",
        "apiVersion": "2017-06-01",
        "properties": {
          "addressPrefix": "[parameters('subnetAddressPrefix2')]",
          "privateAccessServices": []
        },
        "dependsOn": [
          "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]"
        ]
      },
      {
        "apiVersion": "2015-06-15",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[concat(parameters('virtualNetworkName'), '-interface1')]",
        "location": "[resourceGroup().location]",
        "dependsOn": [
          "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
        ],
        "properties": {
          "ipConfigurations": [
            {
              "name": "ipconfig1",
              "properties": {
                /*
                "privateIPAllocationMethod": "Static",
                // reserved IP address range for /24 is 1-3, so start with [0 + 4] or more
                "privateIPAddress": "[parameters('vmIP')]",
                */
                "privateIPAllocationMethod": "Dynamic",
                "subnet": {
                  "id": "[variables('subnetRef')]"
                }
              }
            }
          ]
        }
      }
    ],
    "outputs": {}
  }
  </code></pre>
  
  <p>nestedtemplates/OverlaySubnet.json</p>
  
  <pre><code>{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "solutionName": {
        "type": "string",
        "defaultValue": "testing"
      },
      "virtualNetworkName": {
        "type": "string",
        "defaultValue": "[concat(parameters('solutionName'), '-vnet')]"
      },
      "vnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.0.0/16"
      },
      "subnetName": {
        "type": "string",
        "defaultValue": "[concat(parameters('solutionName'),'-subnet2')]"
      },
      "subnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.101.0/24"
      }
    },
    "variables": {
      "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('virtualNetworkName'))]",
      "subnetRef": "[concat(variables('vnetID'),'/subnets/', parameters('subnetName'))]"
    },
    "resources": [
      {
        "apiVersion": "2015-06-15",
        "type": "Microsoft.Network/virtualNetworks",
        "name": "[parameters('virtualNetworkName')]",
        "location": "[resourceGroup().location]",
        "dependsOn": [],
        "properties": {
          "addressSpace": {
            "addressPrefixes": [
              "[parameters('vnetAddressPrefix')]"
            ]
          },
          "subnets": [
            {
              "name": "[parameters('subnetName')]",
              "properties": {
                "mode": "Incremental",
                "addressPrefix": "[parameters('subnetAddressPrefix')]"
              }
            }
          ],
          "virtualNetworkPeerings": []
        }
      },
      {
        "apiVersion": "2015-06-15",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[concat(parameters('virtualNetworkName'), '-interface2')]",
        "location": "[resourceGroup().location]",
        "dependsOn": [
          "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
        ],
        "properties": {
          "ipConfigurations": [
            {
              "name": "ipconfig1",
              "properties": {
                /*
                "privateIPAllocationMethod": "Static",
                // reserved IP address range for /24 is 1-3, so start with [0 + 4] or more
                "privateIPAddress": "[parameters('vmIP')]",
                */
                "privateIPAllocationMethod": "Dynamic",
                "subnet": {
                  "id": "[variables('subnetRef')]"
                }
              }
            }
          ]
        }
      }
    ],
    "outputs": {}
  }
  </code></pre>