I'm creating an Azure Resource Manager template that instantiates multiple resources, including an Azure storage account and an Azure App Service with a Web App.
I'd like to be able to capture the primary access key (or the full connection string, either way is fine) from the newly-created storage account, and use that as a value for one of the AppSettings for the Web App.
Is that possible?
View account access keys To view and copy your storage account access keys or connection string from the Azure portal: In the Azure portal, go to your storage account. Under Security + networking, select Access keys. Your account access keys appear, as well as the complete connection string for each key.
To create a storage account connection string, we need the account name and one of the keys. As user-defined functions cannot use the reference function or any of the list* functions, we will need to supply these parameters to the function. So, we add accountName and accountKey as parameters .
Call the az storage account keys renew command to regenerate the primary access key. Update the connection strings in your code to reference the new primary access key. Regenerate the secondary access key in the same manner. To regenerate the secondary key, use key2 as the key name instead of key1.
The storage account key is a 512b access key used for authentication when accessing the storage account. It's generated automatically when the storage account is created.
Use the listkeys helper function.
"appSettings": [ { "name": "STORAGE_KEY", "value": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]" } ]
This quickstart does something similar:
https://azure.microsoft.com/en-us/documentation/articles/cache-web-app-arm-with-redis-cache-provision/
The syntax has changed since the other answer was accepted. The error you will now hit is 'Template language expression property 'key1' doesn't exist, available properties are 'keys'
Keys are now represented as an array of keys, and the 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)]",
See: http://samcogan.com/retrieve-azure-storage-key-in-arm-script/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With