0

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.

LionKing
  • 516
  • 5
  • 9
  • I believe this answer on SO already shows it should be possible: https://stackoverflow.com/questions/41004779/create-azure-blob-fileshare-container-through-arm-template/51608344#51608344 Additionally, I think if you were to make the container a linked/nested template, it'd be easier for you to create the containers using the copy statement: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/linked-templates?tabs=azure-powershell It's been a longtime since i used ARM, but this combination should make it possible. Let me know if that helped. – Marco Feb 24 '22 at 12:55
  • Unfortunetly that is not what I am after. as the first link only specify one storage and one container not multiple storage. the second link is for the document of deploying multiple templates together based on outer or inner scope which mens reading variables/parameters from within the template or from the parent. – LionKing Feb 24 '22 at 22:48
  • What do you mean, "apparently still not supported"? The resources section is an array - did you try to put multiple `blobServices/containers` instances in there? – Derek Gusoff Feb 25 '22 at 00:39
  • @DerekGusoff I can create blobService/containers. however, I am not able to create for 2 storage account the same containers let's say I have storage_acct_1 & storage_acct_2 for each of these I need to create 10 blob containers. I am not able to do this without duplicating my storage template and container template – LionKing Feb 25 '22 at 05:45
  • I also pointed out the copy statement. Which should allow you to create multiple resources from the same template. – Marco Feb 25 '22 at 09:18

1 Answers1

1

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?

bmoore-msft
  • 8,376
  • 20
  • 22