ARM 模板命令从不同的资源组中查找资源 ID

ARM template command to find resource id from different resource group

What needs to change in the resourceId('Microsoft.Compute/images/', parameters('imageName')) command below in order to successfully retrieve the id of an image resource that is located in a DIFFERENT resource group?

错误:

当我们 运行 一个包含以下代码的 ARM 模板时,会返回一个错误,表明 "The Image '/subscriptions/long-alpha-numeric-subscription-id/resourceGroups/myCurrentResourceGroup/providers/Microsoft.Compute/images/myimagename' cannot be found in 'westus' region.\ ".

"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-11-01",
"name": "[parameters('VmName')]",
"location": "[parameters('resourceGroupRegion')]",
"properties": {
    "hardwareProfile": { "vmSize": "Standard_DS1_v2" },
    "storageProfile": {
        "imageReference": { 
          "id": "[resourceId('Microsoft.Compute/images/', parameters('imageName'))]"
        },
    ...other stuff

什么有效:

但是,当我们将 resourceId('Microsoft.Compute/images/', parameters('imageName')) 命令替换为我们从 Azure 门户剪切和粘贴的同一区域中不同资源组的预期图像的显式 ID 时,ARM 模板工作正常。

因此图像对 运行 ARM 模板的进程可见,但问题出在 resourceId('Microsoft.Compute/images/', parameters('imageName')) 命令的语法中。

我们需要什么:

如果你仔细看错误信息,它说 "The Image '/subscriptions/long-alpha-numeric-subscription-id/resourceGroups/myCurrentResourceGroup/providers/Microsoft.Compute/images/myimagename' cannot be found in 'westus' region.\"

问题是它在 myCurrentResourceGroup 中查找,而图像位于同一订阅内的 someOtherResourceGroup 中。

What command needs to be entered into the ARM template in order for the ARM template to find the image from the DIFFERENT someOtherResourceGroup resource group without our needing to paste in the raw id value explicitly?

您需要将 someOtherResourceGroup 作为参数并从那里构造 ID。像 :

resourceId(parameters('someOtherResourceGroup'), 'Microsoft.Compute/images', parameters('imageName'))

参见 resourceId() documentation。请注意,此 someOtherResourceGroup 必须在同一订阅中,否则您必须以相同的方式传递订阅 ID。