Retrieve Azure Storage Key in ARM Script
It’s quite a common occurence in an Azure Resource Manager template to be creating a storage account and then need the key for that storage account later in the script. For example I have a template that creates a storage account, then a website and then adds an application setting to that website with a connection string for storage.
Previously to do this you could use the following syntax in an ARM template
"StorageAccount": "[Concat(‘DefaultEndpointsProtocol=https;AccountName=’,variables(‘StorageAccountName’),’;AccountKey=’,listKeys(resourceId(‘Microsoft.Storage/storageAccounts’, variables(‘StorageAccountName’)), providers(‘Microsoft.Storage’, ‘storageAccounts’).apiVersions[0]).key1)]"
This worked fine, until a recent update which changed the keys to now be an array of keys, rather than specific key1 and key 2 objects. As such the working syntax is now
"StorageAccount": "[Concat('DefaultEndpointsProtocol=https;AccountName=',variables('StorageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('StorageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]",