0
votes

I was trying to create an ARM template with a linked template following this doc - https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-tutorial-create-linked-templates

Here is my linked template which I uploaded in a public location

{
   "$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion":"1.0.0.0",
   "parameters":{
     "location":{
         "type":"string",
         "defaultValue":"fakevalue",
         "metadata":{
            "description":"location of deployment."
         }
      },
      "storageName":{
         "type":"string",
         "defaultValue":"[concat('storage', take(uniqueString(subscription().subscriptionId, resourceGroup().id, resourceGroup().name), 5))]",
         "metadata":{
            "description":"The name of the storageAccount"
         }
      },
      "storageSkuName":{
         "type":"string",
         "defaultValue":"Standard_LRS",
         "allowedValues":[
            "Standard_LRS",
            "Standard_GRS",
            "Standard_RAGRS",
            "Standard_ZRS",
            "Premium_LRS"
         ],
         "metadata":{
            "description":"The storage SKU name"
         }
      }
   },
   "variables":{
      "storageApiVersion":"2018-11-01"
    },
   "resources":[
      {
         "comments":"Azure storage for general purpose",
         "type":"Microsoft.Storage/storageAccounts",
         "name":"[parameters('storageName')]",
         "apiVersion":"[variables('storageApiVersion')]",
         "location":"[variables('location')]",
         "kind":"StorageV2",
         "sku":{
            "name":"[parameters('storageSkuName')]"
         },
         "properties":{
            "supportsHttpsTrafficOnly":true
         }
      }
   ],
   "outputs":{

   }
}

This is the mainTemplate that I tried to run

{
   "$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   "contentVersion":"1.0.0.0",
   "parameters":{
      "linkedDeploymentName":{
         "type":"string",
         "defaultValue":"[concat('deployment-', resourceGroup().name, '-linked')]",
         "metadata":{
            "description":"linked deployment name."
         }
      },
         "location":{
         "type":"string",
         "defaultValue":"fakevalue",
         "metadata":{
            "description":"location of deployment."
         }
      }
   },
   "variables":{

   },
   "resources":[
      {
         "apiVersion":"2018-02-01",
         "name":"pid-7d82386d-966e-5431-bf88-1e6ccb393300",
         "type":"Microsoft.Resources/deployments",
         "properties":{
            "mode":"Incremental",
            "template":{
               "$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
               "contentVersion":"1.0.0.0",
               "resources":[

               ]
            }
         }
      },
      {
         "apiVersion":"2019-10-01",
         "name":"[parameters('linkedDeploymentName')]",
         "type":"Microsoft.Resources/deployments",
         "properties":{
            "mode":"Incremental",
            "templateLink":{
               "uri":"<linked-template-url>",
               "contentVersion":"1.0.0.0"
            },
            "parameters":{
               "location":{
                  "value":"[parameters('location')]"
               }
            }
         }
      }
   ]
}

This is the params file

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "value": "australiaeast"
    }
  }
}

When I try to deploy this using

New-AzResourceGroupDeployment -Name "est-deployment" -ResourceGroupName "test-rg"  -TemplateFile .\template4.json -TemplateParameterFile .\params1.json

the error I get is

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template variable 'location' is not found. Please see https://aka.ms/arm-template/#variables for usage details.'.
At line:1 char:5

How can I pass parameter from my main template into the linked template?

2

2 Answers

2
votes

this (in the linked template storage account):

"location":"[variables('location')]",

should be this:

"location":"[parameters('location')]",

because you are using a parameter, not a variable. so it should look like this:

"comments":"Azure storage for general purpose",
"type":"Microsoft.Storage/storageAccounts",
"name":"[parameters('storageName')]",
"apiVersion":"[variables('storageApiVersion')]",
"location":"[parameters('location')]",
"kind":"StorageV2",
0
votes

I would assume you are having issues in your resources "object" by referencing location as a variable instead of a parameter - as you have defined:

"resources":[
  {
     "comments":"Azure storage for general purpose",
     "type":"Microsoft.Storage/storageAccounts",
     "name":"[parameters('storageName')]",
     "apiVersion":"[variables('storageApiVersion')]",
     "location":"[parameters('location')]",  <-- THE CHANGE IS HERE
     "kind":"StorageV2",
     "sku":{
        "name":"[parameters('storageSkuName')]"
     },
     "properties":{
        "supportsHttpsTrafficOnly":true
     }
  }

],