ARM模板中如何根据条件部署同类型不同属性的资源?

How to deploy resources of the same type with different properties based on conditions in ARM templates?

根据资源组,我们希望对 Azure 函数应用使用不同的托管选项(例如 DEV:消费,INT:高级)。但是,我不能在 ARM 模板中两次声明相同的资源。如何确保根据给定的环境参数部署正确的托管计划?

文档解释了如何部署资源或不部署资源,但没有说明如何根据条件使用不同的道具创建资源:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/conditional-resource-deployment

我尝试了以下方法:

{
            "type": "Microsoft.Web/serverfarms",
            "condition": "[equals(parameters('skuTier'), 'Dynamic')]",
            "apiVersion": "2018-02-01",
            "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
            "location": "[resourceGroup().location]",
            "kind": "linux",
            "properties": {
                "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
                "computeMode": "Dynamic"
            },
            "sku": {
                "tier": "[parameters('skuTier')]",
                "name": "[variables('skuName')]"
            },
            "tags": {
                "Project": "[parameters('fullProjectName')]"
            }
        },
        {
            "type": "Microsoft.Web/serverfarms",
            "condition": "[equals(parameters('skuTier'), 'ElasticPremium')]",
            "apiVersion": "2018-02-01",
            "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
            "location": "[resourceGroup().location]",
            "kind": "linux",
            "properties": {
                "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
                "workerSize": "[parameters('workerSize')]",
                "workerSizeId": "[parameters('workerSizeId')]",
                "numberOfWorkers": "[parameters('numberOfWorkers')]",
                "reserved": true,
                "maximumElasticWorkerCount": "20"
            },
            "sku": {
                "Tier": "[parameters('skuTier')]",
                "Name": "[variables('skuName')]"
            },
            "tags": {
                "Project": "[parameters('fullProjectName')]"
            }
        },

您可以根据 env 类型的条件创建一个新变量。稍后,在资源定义中使用新变量。

例如(确保更新下面示例中的 ... set your settings here like below: 部分):

{
    "variables": {
        "skuInt": {
            ... set your settings here like below:
            "name": "EP1",
            "tier": "ElasticPremium",
            "size": "EP1",
            "family": "EP",
            "capacity": 1
        },
        "skuDev": {
          ... set the various settings here.
        },
        "sku": "[if(equals(parameters('env'),'int'), variables('skuInt'), variables('skuDev'))]",
 }

现在将变量用作"sku": "[variables('sku')]",如下所示

{
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2018-02-01",
            "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
            "location": "[resourceGroup().location]",
            "kind": "linux",
            "properties": {
                "name": "[concat(variables('hostingPlanName'), '-', parameters('skuName'))]",
                "workerSize": "[parameters('workerSize')]",
                "workerSizeId": "[parameters('workerSizeId')]",
                "numberOfWorkers": "[parameters('numberOfWorkers')]",
                "reserved": true,
                "maximumElasticWorkerCount": "20"
            },
            "sku": "[variables('sku')]",
            "tags": {
                "Project": "[parameters('fullProjectName')]"
            }
        }