44

Deploying below template:

https://gist.github.com/rnkhouse/aea0a8fd395da37b19466348b919d620

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "String"
        },
        "virtualNetworkName": {
        "type": "string",
        "metadata": {
            "description": "This is the name of the Virtual Network"
        }
        },
        "networkInterfaceName": {
        "type": "string",
        "metadata": {
            "description": "This is the prefix name of the Network interfaces"
        }
        },
        "loadBalancerName": {
        "type": "string",
        "metadata": {
            "description": "This is the name of the load balancer"
        }
        },
        "adminUsername": {
        "type": "string",
        "metadata": {
            "description": "Admin username"
        }
        },
        "adminPublicKey": {
        "type": "string",
        "metadata": {
            "description": "SSH Public Key"
        }
        },
        "imagePublisher": {
        "type": "string",
        "defaultValue": "Canonical",
        "metadata": {
            "description": "Image Publisher"
        }
        },
        "vmNamePrefix": {
        "type": "string",
        "metadata": {
            "description": "Prefix to use for VM names"
        }
        },
        "imageOffer": {
        "type": "string",
        "defaultValue": "UbuntuServer",
        "metadata": {
            "description": "Image Offer"
        }
        },
        "imageSKU": {
        "type": "string",
        "defaultValue": "14.04.5-LTS",
        "metadata": {
            "description": "Image SKU"
        }
        },
        "vmStorageAccountContainerName": {
        "type": "string",
        "defaultValue": "vhds",
        "metadata": {
            "description": "This is the storage account container name"
        }
        },
        "storageAccountName": {
        "type": "string",
        "metadata": {
            "description": "Storage account name"
        }
        },
        "vmSize": {
        "type": "string",
        "defaultValue": "Standard_D1",
        "metadata": {
            "description": "This is the allowed list of VM sizes"
        }
        },
        "subnetName": {
            "defaultValue": "subnet-2",
            "type": "String"
        }
    },
    "variables": {
        "availabilitySetName": "[concat(parameters('subnetName'),'-AVSET')]",
        "addressPrefix": "1.0.0.0/16",
        "subnetPrefix": "1.0.2.0/24",
        "storageAccountType": "Standard_LRS",
        "vnetID": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks',parameters('virtualNetworkName'))]",
        "subnetRef": "[concat(variables('vnetID'),'/subnets/',parameters ('subnetName'))]",
        "numberOfInstances": 2,
        "lbID": "[resourceId('Microsoft.Network/loadBalancers',parameters('loadBalancerName'))]"
    },
    "resources": [
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[parameters('storageAccountName')]",
        "location": "[parameters('location')]",
        "properties": {
            "accountType": "[variables('storageAccountType')]"
        }
        },
        {
        "apiVersion": "2016-04-30-preview",
        "type": "Microsoft.Compute/availabilitySets",
        "name": "[variables('availabilitySetName')]",
        "location": "[parameters('location')]",
        "properties": {
            "platformFaultDomainCount": "2",
            "platformUpdateDomainCount": "2",
            "managed": "true"
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/virtualNetworks/subnets",
        "name": "[concat(parameters('virtualNetworkName'), '/', parameters('subnetName'))]",
        "location": "[parameters('location')]",
        "properties": {
            "addressPrefix": "[variables('subnetPrefix')]"
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/networkInterfaces",
        "name": "[concat(parameters('networkInterfaceName'), copyindex())]",
        "location": "[parameters('location')]",
        "copy": {
            "name": "nicLoop",
            "count": "[variables('numberOfInstances')]"
        },
        "dependsOn": [
            "[concat('Microsoft.Network/loadBalancers/', parameters('loadBalancerName'))]"
        ],
        "properties": {
            "ipConfigurations": [
            {
                "name": "ipconfig1",
                "properties": {
                "privateIPAllocationMethod": "Dynamic",
                "subnet": {
                    "id": "[variables('subnetRef')]"
                },
                "loadBalancerBackendAddressPools": [
                    {
                    "id": "[concat(variables('lbID'), '/backendAddressPools/BackendPool1')]"
                    }
                ]
                }
            }
            ]
        }
        },
        {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/loadBalancers",
        "name": "[parameters('loadBalancerName')]",
        "location": "[parameters('location')]",
        "dependsOn": [],
        "properties": {
            "frontendIPConfigurations": [
            {
                "properties": {
                "subnet": {
                    "id": "[variables('subnetRef')]"
                },
                "privateIPAddress": "1.0.2.50",
                "privateIPAllocationMethod": "Static"
                },
                "name": "LoadBalancerFrontend"
            }
            ],
            "backendAddressPools": [
            {
                "name": "BackendPool1"
            }
            ],
            "loadBalancingRules": [
            {
                "properties": {
                "frontendIPConfiguration": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/frontendIpConfigurations/LoadBalancerFrontend')]"
                },
                "backendAddressPool": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/backendAddressPools/BackendPool1')]"
                },
                "probe": {
                    "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('loadBalancerName')), '/probes/lbprobe')]"
                },
                "protocol": "Tcp",
                "frontendPort": 80,
                "backendPort": 80,
                "idleTimeoutInMinutes": 15
                },
                "Name": "lbrule"
            }
            ],
            "probes": [
            {
                "properties": {
                "protocol": "Tcp",
                "port": 80,
                "intervalInSeconds": 15,
                "numberOfProbes": 2
                },
                "name": "lbprobe"
            }
            ]
        }
        },
        {
        "apiVersion": "2016-04-30-preview",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[concat(parameters('vmNamePrefix'), copyindex())]",
        "copy": {
            "name": "virtualMachineLoop",
            "count": "[variables('numberOfInstances')]"
        },
        "location": "[parameters('location')]",
        "dependsOn": [
            "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]",
            "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'), copyindex())]",
            "[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
        ],
        "properties": {
            "availabilitySet": {
            "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
            },
            "hardwareProfile": {
            "vmSize": "[parameters('vmSize')]"
            },
            "osProfile": {
            "computerName": "[concat(parameters('vmNamePrefix'), copyIndex())]",
            "adminUsername": "[parameters('adminUsername')]",
            "linuxConfiguration": {
                "disablePasswordAuthentication": "true",
                "ssh": {
                    "publicKeys": [
                        {
                            "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                            "keyData": "[parameters('adminPublicKey')]"
                        }
                    ]
                }
            }
            },
            "storageProfile": {
            "imageReference": {
                "publisher": "[parameters('imagePublisher')]",
                "offer": "[parameters('imageOffer')]",
                "sku": "[parameters('imageSKU')]",
                "version": "latest"
            },
            "osDisk": {
                "createOption": "FromImage"
            }
            },
            "networkProfile": {
            "networkInterfaces": [
                {
                "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('networkInterfaceName'),copyindex()))]"
                }
            ]
            },
            "diagnosticsProfile": {
            "bootDiagnostics": {
                "enabled": "true",
                "storageUri": "[concat('http://',parameters('storageAccountName'),'.blob.core.windows.net')]"
            }
            }
        }
        }
    ]
}

Error:

"Deployment failed with status code: 400 and message: Deployment template validation failed: 'The resource 'Microsoft.Network/virtualNetworks/mtes-dev-VNET' is not defined in the template. Please see https://aka.ms/arm-template for usage details.'."

I have already created Virtual Network in other template and using same resource group here. But, still I am getting above error. Please advice!

RNK
  • 5,582
  • 11
  • 65
  • 133
  • 1
    Is there any chance the template could be included in the question, rather than being provided as a link to another site? At some point that link could become broken, making this question less valuable; but also some people may not be able to follow that link, as it may be "blocked" (which is the case for me). – Richardissimo Sep 23 '21 at 13:01

4 Answers4

98

Remove dependsOn the Vnet in your code, it is only needed if that resource is part of the template, not if its already deployed.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
80

For anyone else ending up here from a search for 'The resource is not defined in the template', another possible reason for this error message is a reference of the form:

reference('<some complete id outside this template>')

or

listkeys('<some complete id outside this template>')

The error message doesn't tell you but you need to include the API version when referencing a resource defined outside the current template.

e.g.

reference('<some complete id outside this template>', '2018-03-01')
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • Well spotted. The docs do cover this but it is only in the [https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#reference](description of that parameter): "API version of the specified resource. Include this parameter when the resource isn't provisioned within same template. " – oatsoda Oct 18 '19 at 11:34
  • 5
    This was the reason for the error for me as well. Just wanted to add that instead of a fixed Api version, you can dynamically get it using form example: `providers('Microsoft.Web', 'certificates').apiVersions[0]` – Céryl Wiltink Jun 23 '21 at 10:10
  • 2
    @CérylWiltink, this is a dangerous piece of advice. The API versions are designed to be hardcoded. Your template has been coded against a specific API version and the API version will be supported for some time. During an upgrade, you may need to change the structure of the properties, add some properties that started being required, etc. It is not possible to just change the API version and expect that the template will keep working. – Palec Jan 16 '23 at 10:51
4

I came across this question searching for the same error code. I had a different problem though: I was referencing child resources of another resource within the template. I guess those are considered outside of the current template.

e.g.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "resources": [    
        {
            "type": "Microsoft.Network/virtualNetworks",
            "name": "vnetName",
            "location": "[resourceGroup().location]",
            "apiVersion": "2018-11-01",
            "properties": {
                ...
                }
            },
            "resources": [
                {
                    "type": "subnets",
                    "apiVersion": "2018-11-01",
                    "name": "subnetName",
                    "dependsOn": [
                        "[resourceId('Microsoft.Network/virtualNetworks', vnetName)]"
                    ],
                    "properties": {
                        ...
                    }
                }
            }
        },
        {
            "apiVersion": "2016-02-01",
            "name": "deploymentName",
            "type": "Microsoft.Resources/deployments",
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks.subnets', vnetName, subnetName)]"
            ],
        }
    ]
}

There the fix is to put the parent resource into a deployment and depend on that.

e.g.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",
    "resources": [    
        {
            "apiVersion": "2016-02-01",
            "name": "deployment1",
            "type": "Microsoft.Resources/deployments",
            "resources": [
                {
                    "type": "Microsoft.Network/virtualNetworks",
                    "name": "vnetName",
                    "location": "[resourceGroup().location]",
                    "apiVersion": "2018-11-01",
                    "properties": {
                        ...
                    },
                    "resources": [
                        {
                            "type": "subnets",
                            "apiVersion": "2018-11-01",
                            "name": "subnetName",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/virtualNetworks', vnetName)]"
                            ],
                            "properties": {
                                ...
                            }
                        }
                    ]
                }
            ]
        },
        {
            "apiVersion": "2016-02-01",
            "name": "deployment2",
            "type": "Microsoft.Resources/deployments",
            "dependsOn": [
                "deployment1"
            ],
        }
    ]
}
Jesse Jutson
  • 143
  • 1
  • 7
  • awesome. The answer worked but any explanation why it does not work when I specify [resourceid('Microsoft.Resources/deployments','deployment1')] – ShresthaGanesh Mar 04 '22 at 02:24
1

I had the same issue, basically the solution is instead of:

"dependsOn": [
  resourceid('Microsoft.Resources/deployments','deployment1')]
]

use should use:

"dependsOn": [
  "deployment1"
],
Jorge Freitas
  • 790
  • 10
  • 18