Can I loop over properties in ARM templates?

Lee G. picture Lee G. · Jan 26, 2017 · Viewed 11k times · Source

I have an ARM template where I set up a load balancer and I want to add a number of port openings by adding rules and probes to the LB.

This is the template I have so far:

    {
        "type": "Microsoft.Network/loadBalancers",
        "name": "LB-front",
        "apiVersion": "2016-03-30",
        "location": "westeurope",
        "tags": { },
        "properties": {
            "frontendIPConfigurations": [
                {
                    "name": "LoadBalancerIPConfig",
                    "properties": {
                        "privateIPAllocationMethod": "Dynamic",
                        "publicIPAddress": {
                            "id": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_lbipdev_0_name'))]"
                        }
                    }
                }
            ],
            "backendAddressPools": [
                {
                    "name": "LoadBalancerBEAddressPool"
                }
            ],
            "loadBalancingRules": [
                {
                    "name": "AppPortLBRule1",
                    "properties": {
                        "frontendIPConfiguration": {
                            "id": "[parameters('loadBalancers_LB_dev_id_6')]"
                        },
                        "frontendPort": 80,
                        "backendPort": 80,
                        "enableFloatingIP": false,
                        "idleTimeoutInMinutes": 5,
                        "protocol": "Tcp",
                        "loadDistribution": "Default",
                        "backendAddressPool": {
                            "id": "[parameters('loadBalancers_LB_dev_id_7')]"
                        },
                        "probe": {
                            "id": "[parameters('loadBalancers_LB_dev_id_8')]"
                        }
                    }
                },
                {
                    "name": "AppPortLBRule2",
                    "properties": {
                        "frontendIPConfiguration": {
                            "id": "[parameters('loadBalancers_LB_dev_id_9')]"
                        },
                        "frontendPort": 81,
                        "backendPort": 81,
                        "enableFloatingIP": false,
                        "idleTimeoutInMinutes": 5,
                        "protocol": "Tcp",
                        "loadDistribution": "Default",
                        "backendAddressPool": {
                            "id": "[parameters('loadBalancers_LB_dev_id_10')]"
                        },
                        "probe": {
                            "id": "[parameters('loadBalancers_LB_dev_id_11')]"
                        }
                    }
                },
                {
                    "name": "AppPortLBRule3",
                    "properties": {
                        "frontendIPConfiguration": {
                            "id": "[parameters('loadBalancers_LB_dev_id_12')]"
                        },
                        "frontendPort": 82,
                        "backendPort": 82,
                        "enableFloatingIP": false,
                        "idleTimeoutInMinutes": 5,
                        "protocol": "Tcp",
                        "loadDistribution": "Default",
                        "backendAddressPool": {
                            "id": "[parameters('loadBalancers_LB_dev_id_13')]"
                        },
                        "probe": {
                            "id": "[parameters('loadBalancers_LB_dev_id_14')]"
                        }
                    }
                }
            ],
            "probes": [
                {
                    "name": "AppPortProbe1",
                    "properties": {
                        "protocol": "Tcp",
                        "port": 80,
                        "intervalInSeconds": 5,
                        "numberOfProbes": 2
                    }
                },
                {
                    "name": "AppPortProbe2",
                    "properties": {
                        "protocol": "Tcp",
                        "port": 81,
                        "intervalInSeconds": 5,
                        "numberOfProbes": 2
                    }
                },
                {
                    "name": "AppPortProbe3",
                    "properties": {
                        "protocol": "Tcp",
                        "port": 82,
                        "intervalInSeconds": 5,
                        "numberOfProbes": 2
                    }
                }
            ],
            "inboundNatRules": [],
            "outboundNatRules": [],
            "inboundNatPools": []
        },
        "resources": [],
        "dependsOn": [
            "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_lbipdev_1_name'))]"
        ]
    },

(some details omitted)

What I would like to do is to have an array of the port numbers I want to create rules and probes for and loop over those instead of explicitly having to write each rule and probe as a property for the resource.

Basically I would like a parameter or variable in my template like this:

"ports": [ 80, 81, 82, ...]

and that I could loop over this similar to this: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple.

Answer

HobPet picture HobPet · Apr 7, 2018

Indeed you can! Copy does work with properties!

Create a parameter or variable like this (this example will use parameter array):

"lbRules": {
  "type": "array",
  "defaultValue": [
    {
      "name": "httpPort",
      "frontendPort": "80",
      "backendPort": "80",
      "protocol": "tcp"
    },
    {
      "name": "customAppPort",
      "frontendPort": "8080",
      "backendPort": "8888",
      "protocol": "tcp"
    },
    {
      "name": "httpsPort",
      "frontendPort": "443",
      "backendPort": "443",
      "protocol": "tcp"
    }
  ]
}

Use this parameter in the Loadbalancer resource using copy like this that will create that many probes and rules that you defined in your parameter array:

{
  "apiVersion": "[variables('lbApiVersion')]",
  "type": "Microsoft.Network/loadBalancers",
  "name": "[parameters('myLoadBalancer')]",
  "location": "[parameters('computeLocation')]",
  "dependsOn": [
    "[concat('Microsoft.Network/publicIPAddresses/',concat(parameters('lbIPName'),'-','0'))]"
  ],
  "properties": {
    "frontendIPConfigurations": [
      {
        "name": "LoadBalancerIPConfig",
        "properties": {
          "publicIPAddress": {
            "id": "[resourceId('Microsoft.Network/publicIPAddresses',concat(parameters('lbIPName'),'-','0'))]"
          }
        }
      }
    ],
    "backendAddressPools": [
      {
        "name": "LoadBalancerBEAddressPool",
        "properties": {}
      }
    ],

    "copy": [
      {
        "name": "probes",
        "count": "[length(parameters('lbRules'))]",
        "input": {
          "name": "[concat(parameters('lbRules')[copyIndex('probes')].name,'Probe')]",
          "properties": {
            "intervalInSeconds": 5,
            "numberOfProbes": 2,
            "port": "[parameters('lbRules')[copyIndex('probes')].backendPort]",
            "protocol": "[parameters('lbRules')[copyIndex('probes')].protocol]"

          }
        }
      },

      {
        "name": "loadBalancingRules",
        "count": "[length(parameters('lbRules'))]",
        "input": {
          "name": "[parameters('lbRules')[copyIndex('loadBalancingRules')].name]",
          "properties": {
            "frontendIPConfiguration": {
              "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('myLoadBalancer')),'/frontendIPConfigurations/LoadBalancerIPConfig')]"
            },
            "frontendport": "[parameters('lbRules')[copyIndex('loadBalancingRules')].frontendport]",
            "backendport": "[parameters('lbRules')[copyIndex('loadBalancingRules')].backendport]",
            "enableFloatingIP": false,
            "idleTimeoutInMinutes": "5",
            "protocol": "[parameters('lbRules')[copyIndex('loadBalancingRules')].protocol]",
            "backendAddressPool": {
              "id": "[concat(resourceId('Microsoft.Network/loadBalancers', parameters('myLoadBalancer')),'/backendAddressPools/LoadBalancerBEAddressPool')]"
            },
            "probe": {
              "id": "[concat(variables('lbID0'),'/probes/', parameters('lbRules')[copyIndex('loadBalancingRules')].name,'Probe')]"
            }
          }
        }
      }
    ],


    "inboundNatPools": []
  },

  }
}

More info can be found here: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple#property-iteration