Error & Fix: ARM Linked Template ‘Invalid Template .. could not find template resource’

I will explain an error I encountered and my fix that I find worthwhile sharing and hope that it will save others some time.

In my ARM template development, I am trying to deploy an Azure SQL Server PaaS resource and Key Vault. This is so that I can store the DB admin password in the key vault as a secret. The below ARM template snippet results in the following deployment error.

Code    : InvalidTemplate

Message : Deployment template validation failed: ‘The template reference ‘rkfunctionapp-keyvault’ is not valid: could not find template resource or resource copy with this name. Please see https://aka.ms/arm-template-expressions/#reference for usage details.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"resources": [
    {
      "type": "Microsoft.Sql/servers",
      "name": "[variables('sqlserverName')]",
      "location": "[parameters('location')]",
      "tags": {
        "displayName": "SqlServer"
      },
      "apiVersion": "2014-04-01",
      "dependsOn": [
        "[variables('keyvaultName')]"
      ],
      {
      "apiVersion": "2018-05-01",
      "name": "linkedTemplate",
      "type": "Microsoft.Resources/deployments",
      "properties": {
          "mode": "incremental",
          "templateLink": {
              "uri": "[uri(deployment().properties.templateLink.uri, 'azuredeploy-rkazurekeyvault.json')]",
              "contentVersion": "1.0.0.0"
          },
          "parameters": {
            "keyVaultName": {
              "value": "[variables('keyvaultName')]"
            },
            "objectId": {
              "value": "[parameters('objectId')]"
            },
            "enabledForTemplateDeployment": {
              "value": true
            }
        }
      }
    }

This error occurs when I deploy the template or test the deployment.

For example:

Test-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile .\arm-functionapp-sql-main.json -TemplateParameterFile .\armdeploy-functionappsql.paramaters.json

I troubleshooted long and hard. I even referenced the documentation
Resolve errors for invalid template without much help.

Fix/Resolution:

The fix was to remove keyvaultName dependency
“dependsOn”: [
“[variables(‘keyvaultName’)]”
],

My intention with this was so that the key vault resource would be provisioned prior to the SQL Server resource. The error is correct in stating ‘The template reference ‘rkfunctionapp-keyvault’ is not valid: could not find template resource or resource copy with this name.’ because it had not been created yet.

So the lesson learned is that other syntax can refer to a resource such as dependsOn as opposed to the reference function. So keep an open mind and eyes peeled as this error message is quite vague at times.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s