azureazure-resource-managerazure-rm-template

How to obtain resource group name when using New-AzureRmDeployment and linked ARM template to create a resource group and resource?


When deploying a linked ARM templated via a master ARM template using the New-AzureRmDeployment commandlet, I am getting an error when trying to use the resourceGroup().name function and property. The error message is:

Unable to process template language expressions for resource
'/subscriptions/<subscriptionGuid>/resourceGroups/Zxy- 
Test/providers/Microsoft.Resources/deployments/storageDeployment' at line 
'29' and column '5'. 'The template function
'RESOURCEGROUP' is not expected at this location.`

As an alternative, I have tried using the resourceId(...) function without the resource group function call but that gives an incorrect resource ID where the resource group information is missing and does not match the resource ID obtained from the Azure portal.

e.g.

"value": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"

Result with missing resource group:

/subscriptions/<subscriptionGuid>/providers/Microsoft.Storage/storageAccounts/linktestdata

Here is what I expect, which is also reported by the Azure portal

/subscriptions/<subscriptionGuid>/resourceGroups/Zxy-Test/providers/Microsoft.Storage/storageAccounts/linktestdata

I have the following TestMaster.json and TestLinked0.json templates as an example to demonstrate the problem.

TestMaster.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "linkedTemplateUri": {
      "type": "string",
      "metadata": {
        "description": "URI to the linked ARM template file."
      }
    }
  },
  "resources": [
    {
      "apiVersion": "2018-05-01",
      "name": "testMasterDeployment",
      "type": "Microsoft.Resources/deployments",
      "location": "West US",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[parameters('linkedTemplateUri')]",
          "contentVersion": "1.0.0.0"
        },
        "parameters": {
          "tagValues": { "value": {
              "TagA": "A-Tag",
              "TagB": "B-Tag"
          }}
        }
      }
    }
  ],
  "outputs": {
    "messageFromMaster00": {
      "type": "string",
      "value": "Master-00 reporting"
    },
    "messageFromLinkedTemplate": {
      "type": "object",
      "value": "[reference('testMasterDeployment').outputs.messageFromNestedTemplate]"
    }
  }
}

TestLinked0.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "rgName": {
      "defaultValue": "Zxy-Test",
      "type": "string"
    },
    "location": {
      "defaultValue": "West US",
      "type": "string"
    },
    "tagValues": {
      "type": "object"
    }
  },
  "variables": {
    "storageName": "linktestdata"
  },
  "resources": [
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2018-05-01",
      "location": "[parameters('location')]",
      "name": "[parameters('rgName')]",
      "properties": {},
      "tags": "[parameters('tagValues')]"
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2017-05-10",
      "name": "storageDeployment",
      "resourceGroup": "[parameters('rgName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2017-10-01",
              "name": "[variables('storageName')]",
              "location": "[parameters('location')]",
              "tags": "[parameters('tagValues')]",
              "kind": "StorageV2",
              "sku": {
                "name": "Standard_LRS"
              }
            }
          ],
          "outputs": {
            "storageAccount": {
              "type": "string",
              "value": "[variables('storageName')]"
            },
            "resourceInfo0": {
              "type": "string",
              "value": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
            },
            "resourceInfo1": {
              "type": "string",
              "value": "[resourceId(resourceGroup().name, 'Microsoft.Storage/storageAccounts', variables('storageName'))]"
            }
          }
        }
      }
    }
  ],
  "outputs": {
    "messageFromNestedTemplate": {
      "type": "object",
      "value": "[reference('storageDeployment').outputs]"
    }
  }
}

The PowerShell used to deploy the ARM templates:

$uri = 'https://<containername>.blob.core.windows.net/azure-resource-templates/TestMaster.json'
$linkedTemplateUri = 'https://<containername>.blob.core.windows.net/azure-resource-templates/TestLinked0.json'
New-AzureRmDeployment -Location 'West US' -TemplateUri $uri -linkedTemplateUri $linkedTemplateUri

Solution

  • I have now found the Microsoft documentation that says "The resourceGroup() function is not supported." for subscription level deployments at https://learn.microsoft.com/en-us/azure/azure-resource-manager/deploy-to-subscription#using-template-functions. The commandlet I am using is New-AzureRmDeployment, which is used to deploy resources at the current subscription scope. Looks like I'll need to take a different approach.