当资源在主要位置不可用时,如何在 Azure 资源管理器模板中的次要位置部署资源?

How to deploy a resource in a secondary location in an Azure Resource Manager Template when it is unavailable in the primary location?

我有一个 ARM 模板,用于在给定资源组的位置创建资源。以下是部署 SignalR 服务的方法:

    {
        "apiVersion": "2018-10-01",
        "name": "[variables('signalRName')]",
        "type": "Microsoft.SignalRService/signalR",
        "location": "[resourceGroup().location]",
        "tags": {},
        "sku": {
            "name": "Free_F1",
            "tier": "Free"
        },
        "properties": { }
   }

在加拿大,我可以访问两个位置:Canada EastCanada Central。但是 SignalR 在 Canada Central 中尚不可用,但在 Canada East:

ew-AzResourceGroupDeployment: 11:40:32 AM - Error: Code=LocationNotAvailableForResourceType; Message=The provided location 'canadacentral' is not available for resource type 'Microsoft.SignalRService/SignalR'. List of available regions for the resource type is 'eastus,westus,southeastasia,westeurope,westus2,eastus2,northeurope,australiaeast,canadaeast,centralus,japaneast,uksouth,southcentralus,brazilsouth,francecentral,koreacentral'.

问题

如何将主要位置中不可用的资源部署到一些辅助/备用位置?

你不能那样做,你必须事先知道这一点,或者只是在 arm 模板之外处理故障并重新部署到另一个区域。大多数服务都提供了一个 api 端点来确定支持哪些位置

我找到了一种以牺牲一些调整为代价来实现它的方法。首先,我必须在我的参数文件中为自己指定一个辅助位置参数。

例如,在以下参数文件中,我将 Canada Central 指定为主要位置,将 Canada East 指定为次要位置:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "primaryLocation": {
                "value": "Canada Central"
            },
            "secondaryLocation": {
                "value": "Canada East"
            }
            ...
        }
    }

此参数在我的部署模板文件中定义:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "primaryLocation": {
                "type": "string",
                "minLength": 3,
                "maxLength": 32
            }
            "secondaryLocation": {
                "type": "string",
                "minLength": 3,
                "maxLength": 32
            }
        }
        ... }

然后我发现了给定资源类型的 providers statement. This statement returns the list of locations 可用。

所以我更新了 location 属性 以在 SignalR 可用位置列表中查找资源组的位置。 If it is contained 在列表中,然后我使用资源组的位置。否则,我使用提供的辅助位置。

    {
        "apiVersion": "2018-10-01",
        "name": "[variables('signalRName')]",
        "type": "Microsoft.SignalRService/signalR",
        "location": "[if(contains(providers('Microsoft.SignalRService', 'signalR').locations, parameters('primaryLocation')), parameters('primaryLocation'), parameters('secondaryLocation'))]",
        "tags": {},
        "sku": {
            "name": "Free_F1",
            "tier": "Free"
        },
        "properties": { }
    }

备注

我无法使用 resourceGroup().location,因为它 return 位置的格式与 providers('Microsoft.SignalRService', 'signalR').locations 不同。第一个将 return 类似于 canadaeast 而第二个 returns Canada East.