Search code examples
azurearmazure-resource-manager

Nested resources for nested resources


I am building an ARM template that creates multiple storage accounts and each one of them will contain multiple containers “blobs” but apparently still not supported. Is there any other way to do this beside specify each of them separately?

example of what I am trying to achieve:

StorageAcct_1: must contain 10 blobs StorageAcct_2 : must contain 6 blobs

I am not able to achieve that without duplicating my storage account and container templates.


Solution

  • You can do it - there are multiple ways (nesting, inline, variable loops), it really depends on what you want the code to look like and what your input format is... but a simple n*m loop could use this:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "variables": {
        "numberOfAccounts": 2,
        "blobsPerAccount": 3,
        "saprefix": "[uniqueString(resourceGroup().id)]"
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2021-08-01",
          "name": "[format('{0}{1}', variables('saprefix'), copyIndex())]",
          "location": "[resourceGroup().location]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "StorageV2",
          "copy": {
            "name": "storageAccountLoop",
            "count": "[variables('numberOfAccounts')]"
          }
        },
    
        {
          "type": "Microsoft.Storage/storageAccounts/blobServices",
          "apiVersion": "2021-08-01",
          "name": "[format('{0}{1}/default', variables('saprefix'), copyIndex())]",
          "copy": {
            "name": "blobServiceLoop",
            "count": "[variables('numberOfAccounts')]"
          },
          "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts', format('{0}{1}', variables('saprefix'), copyIndex()))]"
          ]
        },
        {
          "type": "Microsoft.Storage/storageAccounts/blobServices/containers",
          "apiVersion": "2021-08-01",
          "name": "[format('{0}{1}/{2}/{3}{4}', variables('saprefix'), mod(copyIndex(), variables('numberOfAccounts')), 'default', 'container', mod(copyIndex(), variables('blobsPerAccount')))]",
          "copy": {
            "name": "containerLoop",
            "count": "[mul(variables('numberOfAccounts'), variables('blobsPerAccount'))]"
          },
          "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts', format('{0}{1}', variables('saprefix'), mod(copyIndex(), variables('numberOfAccounts'))))]",
            "[resourceId('Microsoft.Storage/storageAccounts/blobServices', format('{0}{1}', variables('saprefix'), mod(copyIndex(), variables('numberOfAccounts'))), 'default')]"
          ]
        }
      ]
    }
    
    

    That help?