简体   繁体   中英

Nested loops or Cartezian product of arrays in Azure ARM

I'm building an ARM(Azure Resource Manager) template to create multiple resources of the same type. Let's say metric alerts for SQL servers. I have:

  1. 3 severity levels: [1, 2, 3]
  2. 20 servers with names [sqlserver_1, sqlserver_2, ...]
  3. 3 metrics to monitor [memory, cpu load, number of connections]

Essentially, I need a total of 180 resources. Is there any way how I can build and with all possible combinations of these variables. Ie for each of the servers, I need to monitor 3 metrics where each could trigger 3 possible alerts levels depending on the metric levels.

Naturally, I thought about a Cartesian product of these arrays and then a copy loop over it to fill the template attributes. However, it doesn't look like ARM supports this.

Is it the point where instead of using ARM I should think about writing a code-generator to create a template instead of trying to bend ARM json?

Regarding the issue, you can add the copy element to the resources section of your template. After doing that, you can dynamically set the number of resources to deploy. For more details, please refer to here and here .

For example

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "org": {
            "type": "array",
            "defaultValue": [
                "contoso",
                "fabrikam",
                "coho"
            ]
        }
    },
    "resources": [
        {
            "apiVersion": "2017-06-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat(parameters('org')[copyIndex()], uniqueString(resourceGroup().id))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": "[length(parameters('org'))]"
            }
        }
    ],
    "outputs": {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM