Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

                    }
                }
            ]
        }
    ]
}

like image 848
NoOneSpecial Avatar asked Apr 21 '15 17:04

NoOneSpecial


People also ask

What is Resourceid in Azure?

Every Azure Resource Manager resource has an associated resource ID that uniquely identifies it. Certain operations require that you provide the resource ID. You can get the resource ID for a storage account by using the Azure portal, PowerShell, or Azure CLI.

What language are Azure Resource Manager templates written in?

Azure Resource Manager templates are JavaScript Object Notation (JSON) files that define the infrastructure and configuration for your project.

How do I get a tenant ID for my Azure ARM template?

The tenantId is now available with template functions. Use the subscription function: subscription(). Save this answer.

How do I get the resource ID in ARM template?

The first way is to look for it on this Microsoft Azure resource page here; the second option is when using ARM Templates, just look at the type line of the resource and you will find it there; a third and easy way to spot the option is to check the id of the object. It provides you the resource type on it.


1 Answers

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

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

like image 84
Jordan Locke Avatar answered Nov 03 '22 01:11

Jordan Locke