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')]" } } ] } ] }
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.
Azure Resource Manager templates are JavaScript Object Notation (JSON) files that define the infrastructure and configuration for your project.
The tenantId is now available with template functions. Use the subscription function: subscription(). Save this answer.
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.
I stumbled upon the solution in a completely unrelated article, but you should be passing
[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With