Azure ARM template
1. Iteration
-
You can create multiple instances of a resource by adding a copy loop. You can add a copy loop to any four sections: resources, properties, variables, and outputs.
-
You cannot use a copy loop in the child resource to create multiple subnets.
1.1. Resource iteration in ARM templates
Add the copy element to the resources section of your template to deploy multiple instances of the resource. The copy element has the following general format:
"copy": {
"name": "<name-of-loop>",
"count": <number-of-iterations>,
"mode": "serial" <or> "parallel",
"batchSize": <number-to-deploy-serially>
}
The following example creates the number of storage accounts specified in the storageCount parameter.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"storageCount": {
"type": "int",
"defaultValue": 3
}
},
"resources": [
{
"copy": {
"name": "storagecopy",
"count": "[length(range(0, parameters('storageCount')))]"
},
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[format('{0}storage{1}', range(0, parameters('storageCount'))[copyIndex()], uniqueString(resourceGroup().id))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
}
]
}
1.2. Property iteration in ARM templates
Add the copy element to the resources section of your template to set the number of items for a property.
"copy": [
{
"name": "<name-of-property>",
"count": <number-of-iterations>,
"input": <values-for-the-property>
}
]
The input property specifies the properties that you want to repeat. You create an array of elements constructed from the value in the input property.
Example:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"numberOfDataDisks": {
"type": "int",
"minValue": 0,
"maxValue": 16,
"defaultValue": 3,
"metadata": {
"description": "The number of dataDisks to create."
}
},
...
},
"resources": [
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2022-11-01",
...
"properties": {
"storageProfile": {
...
"copy": [
{
"name": "dataDisks",
"count": "[parameters('numberOfDataDisks')]",
"input": {
"lun": "[copyIndex('dataDisks')]",
"createOption": "Empty",
"diskSizeGB": 1023
}
}
]
}
...
}
}
]
}
2. Modes
2.1. Incremental mode
- When redeploying an existing resource in incremental mode, all properties are reapplied.
- Properties that aren't included in the template are reset to the default values.
| Create | Update | Delete | |
|---|---|---|---|
| Resource | Y | Y | No |
| Child resource | Y | Y | No |
| Properties of a resource | Y | Y | Y |
2.2. Complete mode
-
In complete mode, Resource Manager deletes resources that exist in the resource group but aren't specified in the template.
-
There are some differences in how resource types handle complete mode deletions. Parent resources are automatically deleted when not in a template that's deployed in complete mode. Some child resources aren't automatically deleted when not in the template. However, these child resources are deleted if the parent resource is deleted.
| Create | Update | Delete | |
|---|---|---|---|
| Resource | Y | Y | Y |
| Child resource | Y | Y | No |
| Properties of a resource | Y | Y | Y |
For example, if your resource group contains a DNS zone (`Microsoft.Network/dnsZones` resource type) and a CNAME record (`Microsoft.Network/dnsZones/CNAME` resource type), the DNS zone is the parent resource for the CNAME record. If you deploy with complete mode and don't include the DNS zone in your template, the DNS zone and the CNAME record are both deleted. If you include the DNS zone in your template but don't include the CNAME record, the CNAME isn't deleted.
3. References
- https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/copy-resources
- https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/copy-resources#iteration-for-a-child-resource