0
votes

I have an Azure generated template and parameters file for creating a simple VM. It works fine with the supplied deploy.ps1. I've updated the parameters file to create another VM in a new resource group, but it now fails with an "Invalid resource reference" error.

I've reviewed the template, but not changed it. I can see there's nothing hard-coded. The deployment creates the Network Security Group and the public IP address but then fails on the NIC. I've checked for region issues, but can't see any.

The NIC part of the template looks like this:

"variables": {
        "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
        "vnetId": "[parameters('virtualNetworkId')]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2018-10-01",
            "name": "[parameters('networkInterfaceName')]",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[variables('nsgId')]"
                }
            }
        },

The error message is:

New-AzResourceGroupDeployment : 17:11:30 - Resource Microsoft.Network/networkInterfaces 'newRHserverNIC' failed with message '{ "error": { "code": "InvalidResourceReference", "message": "Resource /subscriptions//resourceGroups/Scripttest2/providers/Microsoft.Network/virtualNetworks/NewvirtNet/subnets/default referenced by resource /subscriptions//resourceGroups/Scripttest2/providers/Microsoft.Network/networkInterfaces/newRHserverNIC was not found. Please make sure that the referenced resource exists, and that both resources are in the same region.", "details": [] } }'

2
Thanks for correcting my spelling. We often don't use commas before conjunctions in the UK as the conjunction is deemed sufficient to indicate that a pause may be appropriate but each to their own. :-)Slartibartfast

2 Answers

0
votes

well, its either of two, subnet is in a different region\different resource group\different subscription or called differently.

0
votes

Don't ignore <azuredeploy.parameters.json> file. In my case, this file was faulty. It had a different Location than the current Resource Group, because I extracted it from a previous execution.