0
votes

I have a resource group called network-rg containing a virtual network. I am trying to deploy a Virual Machine in a new resource group vm-rg. The VM should be connected to a new subnet on the vnet in network-rg. I am using one single ARM template with both the subnet and the VM and deploying to vm-rg. How do I specify the subnet in the ARM template when its vnet is in another resource group than the primary/default group for the deployment?

I would need to explicitly reference the vnet with the resource group. This would be similar to how a Network Interface deploy is referencing the subnet ID in its ipConfigurations properties list:

  "apiVersion": "2015-05-01-preview",
  "type": "Microsoft.Network/networkInterfaces",
  "name": "[parameters('nicName')]",
  "location": "[parameters('location')]",
  "properties": {
      "ipConfigurations": [{
          "name": "ipconfig1",
          "properties": {
              "subnet": {
                  "id": "[variables('subnet1Ref')]"
              }
          }
      }]
   }
2
Check out the ARM resourceid function. You can assign this to a variable or stick it directly into the ID. If the subscriptionid is the same then use subscription().subscriptionId docs.microsoft.com/en-us/azure/azure-resource-manager/…The Fish
Thanks @TheFish! The resource group is in the same subscription and I know how to get the ID to the vnet using the resourceId function; "[resourceId(parameters('vnetResourceGroup'), 'Microsoft.Network/virtualNetworks', parameters('vnetName'))]" What I don't understand is how to supply the ID of the vnet resource into the subnet template when creating the subnet.Mattias

2 Answers

1
votes

It seems you cannot create a subnet in another resource group in one template when you create a new resource group with resources. There no property for you to refer to the Vnet in another group.

If you really want to create a new subnet in another group in one template, you could take a look at linked and nested template. Hope this will help you.

0
votes

You will need to add a variables (Resource in the template that references the parameter file) example below:

In the main.JSON file:

{
    "name": "[parameters('networkInterfaceName')]",
    "type": "Microsoft.Network/networkInterfaces",
    "apiVersion": "2018-04-01",
    "location": "[parameters('location')]",
    "dependsOn": [
      "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
    ],
    "properties": {
      "ipConfigurations": [
        {
          "name": "ipconfig1",
          "properties": {
            "subnet": {
              "id": "[variables('subnetRef')]"
            },
            "privateIPAllocationMethod": "Dynamic",
            "publicIpAddress": {
              "id": "[resourceId(parameters('resourceGroupName'),'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
            }
          }
        }
      ],
      "enableAcceleratedNetworking": true,
      "networkSecurityGroup": {
        "id": "[resourceId(parameters('nsgResourceGroupName'), 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
      }
    }
  }   

Add the following variables in main.JSON:

{
    "name": "[parameters('networkInterfaceName')]",
    "type": "Microsoft.Network/networkInterfaces",
    "apiVersion": "2018-04-01",
    "location": "[parameters('location')]",
    "dependsOn": [
      "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
    ],
    "properties": {
      "ipConfigurations": [
        {
          "name": "ipconfig1",
          "properties": {
            "subnet": {
              "id": "[variables('subnetRef')]"
            },
            "privateIPAllocationMethod": "Dynamic",
          }
        }
      ],
    }
  }

Then add the following parameters in the main.JSON file:

"parameters": {
  "virtualNetworkName": {
  "type": "string"
},
"subnetName": {
  "type": "string"
},

In the main.parameters.json add the variable information:

"virtualNetworkName": {
  "value": "<vnet name>"
},
"resourceGroupName": {
  "value": "<whatever the rg is for the Network>"
},
"subnetName": {
  "value": "<subnet name>"
},

Hope that helps.