Search code examples
azureazure-resource-managerazure-rm-template

Copy storage accounts and output connection strings


I need to deploy N storage accounts and output connection strings as array or better comma separated unified string value. I found a very useful article on how to deploy multiple resources. Here is how I can create multiple storage accounts.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2016-01-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": 3
            }
        }
    ],
    "outputs": {}
}

Now the problem is, there is no info on how to iterate through storage accounts to output connection strings. Did anybody do something like this? How can I iterate through deployed storage accounts and output connection strings?


Solution

  • This is a working ARM Template modified based on your sample template above.

    It is able to output the list of partial storage accounts connection strings deployed through ARM template deployment in the deployment output, without the storage account keys.

    This is due to a open and known issue: listKeys not supported in variable #1503 in ARM, where the listKeys to list the storage account key is not allowed to be used in a ARM Template variable.

    Output:

    { "connectionstrings": [ { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=0storageojjbpuu4wl6r4;AccountKey=" }, { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=1storageojjbpuu4wl6r4;AccountKey=" }, { "connectionstring": "DefaultEndpointsProtocol=https;AccountName=2storageojjbpuu4wl6r4;AccountKey=" } ] }

    {
      "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storageAccountCount": {
          "type": "int",
          "defaultValue": 3
        }
      },
      "variables": {
        "storageAccountConnectionStrings": {
          "copy": [
            {
              "name": "connectionstrings",
              "count": "[parameters('storageAccountCount')]",
              "input": {
                "connectionstring": "[concat('DefaultEndpointsProtocol=https;AccountName=', concat(copyIndex('connectionstrings'),'storage', uniqueString(resourceGroup().id)), ';AccountKey=')]"
              }
            }
          ]
        }
      },
      "resources": [
        {
          "apiVersion": "2016-01-01",
          "type": "Microsoft.Storage/storageAccounts",
          "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
          "location": "[resourceGroup().location]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "Storage",
          "copy": {
            "name": "storagecopy",
            "count": "[parameters('storageAccountCount')]"
          }
        }
      ],
      "outputs": {
        "connectionStringsArray": {
          "type": "object",
          "value": "[variables('storageAccountConnectionStrings')]"
        }
      }
    }