Azure Resource Manager Template Language - resourceId(): Unable to evaluate template language function 'resource Id'
Asked Answered
G

1

7

How do you correctly call the function resourceId() defined in the Azure Resource Manager Template Language?

Context

///  - Azure
///  - Azure Resource Management
///      https://msdn.microsoft.com/en-us/library/azure/dn578292.aspx
///
///  - Azure Resource Manager Template Language
///      https://msdn.microsoft.com/en-us/library/azure/dn835138.aspx
///
///  - Azure Resource Manager Template Language functions and expressions
///  - Azure Resource Manager Template Language function:
///      resourceId('resourceNamespace/resourceType','resourceName')
///
///  - Powershell
///  - Azure PowerShell
///  - Azure PowerShell Resource Manager Mode (Switch-AzureMode AzureResourceManager)
///  - Azure PowerShell CmdLet: New-AzureResourceGroup
///

This line in the template (see full template below)
"sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"


Gives this error when running the PowerShell New-AzureResourceGroup CmdLet:

    PS c:\AzureDeployment> New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -DeploymentName "psDeployment" -TemplateFile .\Template.json -TemplateParameterFile .\Parameters.json -Verbose
    cmdlet New-AzureResourceGroup at command pipeline position 1
    Supply values for the following parameters:
    (Type !? for Help.)
    VERBOSE: Performing the operation "Replacing resource group ..." on target "psDeployment".
    VERBOSE: 16:22:07 - Created resource group 'psResourceGroup' in location 'northeurope'
    New-AzureResourceGroup : 16:22:08 - Resource Microsoft.Sql/servers/databases 
    'xxx-sql-server-name-xxx/psDatabaseName' failed with message 
    'Unable to process template language expressions for resource
    '/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
    at line _ and column _. 
    'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).''
    At line:1 char:1
    + New-AzureResourceGroup -Location "North Europe" -Name "psResourceGroup" -Templat ... 
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (:) [ New-AzureResourceGroup ], Exception 
        + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupCommand



The function resourceId() has, according to the documentation, 2 parameters, and I call it with two constant strings that I beleve is correct:
resourceId('Microsoft.Sql/servers/databases', 'TestDB')
Still it produces an error message indicating that the number of parameters is wrong:
'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).'

The resource used, according to the error message, is: '/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'

So, what is the correct way to call resourceId() for a database?

Also, if I remove createMode and sourceDatabaseId from the template everything works fine.



This is the template used above

{    
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json",
    "contentVersion": "1.0.0.0",

    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "North Europe",
            "allowedValues": [
                "East Asia",
                "South East Asia",
                "East US",
                "West US",
                "North Central US",
                "South Central US",
                "Central US",
                "North Europe",
                "West Europe"
            ]
        },
        "sqlServerName": { "type": "string" },
        "sqlAdminUserName": { "type": "string" },
        "sqlAdminUserPassword": { "type": "securestring" },
        "databaseName": { "type": "string" }
    },

    "resources": [
        {
            "type": "Microsoft.Sql/servers",
            "apiVersion": "2.0",
            "location": "[parameters('location')]",
            "name": "[parameters('sqlServerName')]",
            "properties": { "administratorLogin": "[parameters('sqlAdminUserName')]", "administratorLoginPassword": "[parameters('sqlAdminUserPassword')]" },
            "resources": [
                {
                    "type": "databases",
                    "apiVersion": "2.0",
                    "location": "[parameters('location')]",
                    "name": "[parameters('databaseName')]",
                    "dependsOn": [ "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]" ],
                    "properties": {
                        "edition": "Standard",
                        "collation": "SQL_Latin1_General_CP1_CI_AS",
                        "maxSizeBytes": "10737418240",
                        "requestedServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b",
                        "createMode": "Copy",

====>                   "sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"

                    }
                }
            ]
        }
    ]
}

Glacial answered 21/4, 2015 at 17:50 Comment(0)
C
18

I stumbled upon the solution in a completely unrelated article, but you should be passing

[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]

Charest answered 29/1, 2016 at 22:47 Comment(1)
Do you always need to specify the exact name of the resource you want to remediate?Damarisdamarra

© 2022 - 2024 — McMap. All rights reserved.