0
votes

I need to add a subnet to a VNET in another resource group. I have an arm template (below) which adds a subnet to an existing resource group and it works okay but I need this subnet to be added to a VNET in another resource group.

example - there are 2 resource groups Resource Group A - Contains the VNET Resource Group B - VM deployed here but needs to connect to VNET in Resource Group A

When I deploy the template which created the VM to Resource Group B, i need to reference resource group A from within this ARM template, how can I do this? I am deploying via Visual Studio at the moment so I am using right click then Deploy to and selecting the resource group where the VM is being deployed to eg Resource Group B. The below code works but it deploys to the wrong resource group, it should add the subnet to Resource group where the VNET is but adds it to the resource group the VM is being deployed to!

"vnetID": "[resourceId(parameters('ResourceGroupName'),'Microsoft.Network/virtualNetworks',parameters('existingVNETName'))]"

 "apiVersion": "2015-06-15",
  "type": "Microsoft.Network/virtualNetworks/subnets",
  "name": "[concat(parameters('existingVNETName'), '/', parameters('newSubnetName'))]",
  "location": "[resourceGroup().location]",
  "properties": {
    "addressPrefix": "[parameters('newSubnetAddressPrefix')]"
3
Have you creaetd the both resource groups in the same location or different locations?Vikranth S

3 Answers

0
votes

This isn't possible. The subnets in a VNET are properties of that VNET, so you cannot create a subnet in a different resource group.

You can however add users to a certain subnet, so only a certain user could only add machines to his " allowed" subnets. Add users to subnet

0
votes

It is not possible to add a Subnet to a VNET in another resource group, because a Subnet is not a top level resource in Azure. All Subnets within a virtual network always role up to the virtual network resource in your resource group.

-1
votes

Actually it can be done. Using Azure resource explorer I got the id of the subnet I wanted to add the virtual machine to. Then in the resource which builds the NIC (usually the "type": "Microsoft.Network/networkInterfaces"),under the subnet properties you can paste this id. Im working on parametizing this but worked. Azure resource explorer is the tool I used

"name": "NIC",
  "type": "Microsoft.Network/networkInterfaces",
  "location": "[resourceGroup().location]",
  "apiVersion": "2016-03-30",
  "dependsOn": [
  ],
  "tags": {
    "displayName": "[variables('NicName')]"
  },
  "properties": {
    "ipConfigurations": [
      {
        "name": "ipconfig1",
        "properties": {
          "privateIPAddress": "[parameters('primaryPrivateIPAddress')]",
          "privateIPAllocationMethod": "Static",
          "subnet": {
            "id": ""/subscriptions/abcd123456789/resourceGroups/ResourceGroupA/providers/Microsoft.Network/virtualNetworks/ResourceGroupVirtualNetwork/subnets/newsubnet""
          }