Creating multiple resources in loop

We already know how to use the copy feature of the ARM templates to loop and create resources. Azure provides the capability to use even the copy features in the variables as well.

The variables defined within the variables section of the ARM templates are static in their count. If you define five variables, you get just five variables. Earlier, there was no way to have a dynamic number of variables during deployment. Adding copy into the variables helps with achieving defined variables at runtime and using them in ARM templates.

There are two distinct ways to use the copy element within the variables section. The first one generates a JSON object containing an array, while the other generates an array. If the copy element appears at the top level as a variable, it generates an array and assigns it to a variable generated during deployment. The name of the variable is the value provided to the name property. When the copy keyword is used as within a variable name, it generates an object, and creates an inner variable, assigning an array to it.

If you look at the master template azuredeploy.json, you will notice the copy feature used in the variables section, as shown here:

"multiLocation": {
"copy": [
{
"name": "location",
"count": "[length(parameters('resourceGroupInfo'))]",
"input": {
"resourceGroupName": "[concat('RGP','-',parameters('environmentName'),'-', parameters('resourceGroupInfo')[copyIndex('location')].resourceGroupName )]",
"appServicePlanName": "[toLower(concat('asp','-',parameters('resourceGroupInfo')[copyIndex('location')].resourceGroupSuffix,'-',parameters('appServicePlanSkuName'),'-', parameters('environmentName')))]",
"webAppName": "[toLower(concat('web','-',parameters('resourceGroupInfo')[copyIndex('location')].resourceGroupSuffix,'-', parameters('appServicePlanSkuName'),'-', parameters('environmentName')))]"
}
}
]
}

The variable multiLocation does not have a static value; instead, it uses the copy feature. When the copy keyword is used, as within the multiLocation variable, it generates an object and creates an inner variable named location, assigning an array to it. The array comprises of the values generated from the input values.

In the current sample, the length of the resourceGroupInfo object supplied as the parameter is 2. It means the copy element loops twice and generates two objects within a new array. The objects would contain values as shown here:

"multiLocation": {
"location": [
{
"resourceGroupName": "RGP-Dev-eCommerceUS",
"appServicePlanName": "asp-eastus-S1-DEV",
"webAppName": "web-eastus-S1-DEV"
},
{
"resourceGroupName": "RGP-Dev-eCommerceEurope",
"appServicePlanName": "asp-westEurope-S1-DEV",
"webAppName": "web-westEurope-S1-DEV"
}
]
}

The length function, when applied to an array, provides the count of the element. The concat function joins multiple string values together. The parameter value DEV was provided for the environmentName and S1 for the appServicePlanSkuName.

Two objects were generated at runtime that can be referenced within the ARM template, using the syntax as shown here. The syntax accesses the resourceGroupName-generated property, using the location array:

"[variables('multiLocation').location[copyIndex()].resourceGroupName]"

This pattern should be used whenever there is a need to generate multiple variables at runtime, rather than defining them statically at design time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.152.87