Copy Loops for Multiple Resources
You'll learn how to use ARM template copy loops to stamp out multiple similar resources without duplicating code.
What Are Copy Loops?
When you need to deploy multiple instances of the same resource—like five virtual machines or three storage accounts—you could copy-paste the resource declaration five times. But that's tedious, error-prone, and hard to maintain. Copy loops let you specify "create N of these" with a single resource block, varying properties using array iteration.
Think of it like a cookie cutter: instead of shaping each cookie individually, you press the cutter multiple times, slightly adjusting the position or decoration each time.
How Copy Loops Work
In ARM templates, you add a copy property to a resource declaration:
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[concat('storage', copyIndex())]",
"location": "eastus",
"sku": { "name": "Standard_LRS" },
"copy": {
"name": "storageCopy",
"count": 3
}
}
This creates three storage accounts: storage0, storage1, and storage2. The copyIndex() function returns the current iteration number (starting at 0). You can offset it: copyIndex(1) starts at 1.
Varying Properties
Use parameters or variables with copyIndex() to customize each instance:
"name": "[concat(parameters('namePrefix'), copyIndex())]"
You can also reference arrays to pull different values per iteration, combining what you know about parameters and variables.
Key Takeaway:
Copy loops eliminate repetitive resource declarations by iterating a specified count, using copyIndex() to create unique, parameterized instances of resources in ARM templates.