Home Copy element within copy element in Azure ARM deployment template to create multiple instances of a resource
Reply: 1

Copy element within copy element in Azure ARM deployment template to create multiple instances of a resource

Frank Fu
1#
Frank Fu Published in 2017-12-06 05:40:39Z

I'm trying to create do a "nested for loop" in my Azure ARM deployment template via the copy element feature to create multiple instance of the same resource type (Microsoft.Web/sites/hostnameBindings in my case)

More specifically I'm trying to bind multiple hostnames to multiple apps (azure app service websites).

Is this possible? Or would I need to go down the linked templates path?

Here is my attempt so far but I can't get it to work.

parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
    "appList": {
      "value": [
        { "appName": "app1", "hostNames": [ "app1.qqq.example.com", "app1.ttt.example.com" ] },
        { "appName": "app2", "hostNames": [ "app2.qqq.example.com" , "app2.ttt.example.com" ] },
        { "appName": "app3", "hostNames": [ "app3.qqq.example.com", "app3.ttt.example.com" ] }    
      ]
    }    
  }
}

template.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
    "appList": { "type": "array"  }
  },  
  "resources": [
    {
      "type": "Microsoft.Web/sites/hostnameBindings",
      "name": "parameters('appList')[copyIndex('webAppCopy')]/parameters('appList')[copyIndex('webAppCopy')].hostNames",
      "copy": [
        {
          "name": "webAppCopy",
          "count": "[length(parameters('appList'))]"
        }        
      ],
      "apiVersion": "2016-03-01",
      "location": "[resourceGroup().location]"
    }
  ],
  "outputs": {}
}
John Rusk - MSFT
2#
John Rusk - MSFT Reply to 2017-12-06 20:48:42Z

I've never seen anything in the docs about direct support for nested loops. But you could probably solve this with the numeric functions as follows:

  1. define a template variable that is number-of-apps * number-of-addresses-per-app. Let's call this variable bindingCount
  2. Use bindingCount as the count for your "copy"
  3. When constructing the resource name, use the div and mod functions to take copyIndex and turn it back into an "app index" and a "hostname" index. I think you'll have to do the math inline, in the formula that constructs the name.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308767 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO