29
votes

Deploying below template:

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

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!

3

3 Answers

70
votes

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

46
votes

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')
2
votes

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"
            ],
        }
    ]
}