Implementing dependsOn Chain Inside Looped Resources in ARM Templates for Azure Backup
AdPsychological9128 discusses their struggle with enforcing a dependsOn chain within a loop of ARM template resources for Azure file share backup, and seeks community solutions.
Implementing dependsOn Chain Inside Looped Resources in ARM Templates (Azure Backup for File Shares)
Author: AdPsychological9128
Scenario
The author is automating Azure Recovery Services deployments to protect (i.e., back up) Azure file shares with ARM templates. Their goal is to ensure that each resource generated by looping over an array is deployed only after the prior one—enforcing sequential execution using the dependsOn
property.
Objective
- Loop over an array (
protectedItemsArray
) of resources (protected file shares). - Dynamically generate resource IDs and related properties.
- Build each resource’s
dependsOn
property to reference the preceding resource within the same loop, so deployments occur in sequence.
Challenges
It appears that ARM templates do not natively support establishing a dependsOn
chain (i.e., making each looped resource depend directly on the previous loop iteration). Several approaches have been attempted, but all result in validation errors:
Attempted Approaches
-
Returning an Array for First Iteration, String for Others:
For the first array element: try returning an empty array for
dependsOn
. For later elements: try constructing a resource ID referencing the previous item.Example:
"[if(greater(copyIndex(), 0), concat('Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems/', parameters('protectedItemsArray')[sub(copyIndex(), 1)].vaultName, '/Azure/', variables('containerSuffix'), ';', parameters('protectedItemsArray')[sub(copyIndex(), 1)].storageAccountResourceGroup, ';', parameters('protectedItemsArray')[sub(copyIndex(), 1)].storageAccountName, '/AzureFileShare;', parameters('protectedItemsArray')[sub(copyIndex(), 1)].fileShareName), json('[]'))]"
Failure: This approach fails because
json('[]')
returns an array whereas a resource ID string is expected. -
Using
json(null())
or Empty String:"[if(greater(copyIndex(), 0), concat(...), json(null()))]"
Failure:
json(null())
is invalid for this context. -
Returning
json('[]')
,json('')
, orstring('')
:All these variants result in validation errors because ARM requires a valid string for a resource ID in
dependsOn
, not an array or an empty value.
Questions for the Community
- Has anyone successfully implemented a dependsOn chain between looped resources in ARM templates, so each resource depends on the previous?
- Are there established best practices or usable workarounds?
- Is this scenario even supported in ARM templates as of now? If so, any guidance, sample code, or documentation references would be appreciated.
Additional Context
- The author is specifically automating Azure Backup for file shares.
- The focus is on forcing sequential resource deployment inside a loop rather than deploying all resources concurrently.
Request
If you have any solutions, related documentation, or workaround strategies, please share your insights.
Source: Reddit post by u/AdPsychological9128
This post appeared first on “Reddit Azure DevOps”. Read the entire article here