Azure 资源管理器模板语言 - resourceId():无法评估模板语言函数 'resource Id'
Azure Resource Manager Template Language - resourceId(): Unable to evaluate template language function 'resource Id'
如何正确调用Azure Resource Manager Template Language中定义的函数resourceId()?
上下文
/// - 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
///
模板中的这一行(请参阅下面的完整模板)
"sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
当 运行 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
根据 documentation,函数 resourceId()
有 2 个参数,我用我认为正确的两个常量字符串调用它:
resourceId('Microsoft.Sql/servers/databases', 'TestDB')
还是会报错,提示参数个数不对:
'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).'
根据报错信息,使用的资源是:
'/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
那么,为数据库调用 resourceId() 的正确方法是什么?
此外,如果我从模板中删除 createMode 和 sourceDatabaseId,一切正常。
这是上面用到的模板
{
"$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')]"
}
}
]
}
]
}
我在一篇完全不相关的文章中偶然发现了解决方案,但你应该通过
[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]
如何正确调用Azure Resource Manager Template Language中定义的函数resourceId()?
上下文
/// - 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
///
模板中的这一行(请参阅下面的完整模板)
"sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
当 运行 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
根据 documentation,函数 resourceId()
有 2 个参数,我用我认为正确的两个常量字符串调用它:
resourceId('Microsoft.Sql/servers/databases', 'TestDB')
还是会报错,提示参数个数不对:
'Unable to evaluate template language function 'resource Id': the type 'Microsoft.Sql/servers/databases' requires '2' resource name argument(s).'
根据报错信息,使用的资源是:
'/subscriptions/xxxxxxxx/resourceGroups/psResourceGroup/providers/Microsoft.Sql/servers/xxx-sql-server-name-xxx/databases/psDatabaseName'
那么,为数据库调用 resourceId() 的正确方法是什么?
此外,如果我从模板中删除 createMode 和 sourceDatabaseId,一切正常。
这是上面用到的模板
{ "$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')]" } } ] } ] }
我在一篇完全不相关的文章中偶然发现了解决方案,但你应该通过
[resourceId('Microsoft.SQL/servers/databases', parameters('sqlServerName'), 'TestDB')]