The resource is not defined in the template

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


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

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