Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM - How can I get the access key from a storage account to use in AppSettings later in the template?

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?

like image 1000
Scott Avatar asked Oct 19 '15 23:10

Scott


People also ask

How do I find my storage account access key?

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.

How do I get storage account connection string in arm template?

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 .

What are the 2 ways to refresh storage account keys?

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.

What is access account storage key?

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.


2 Answers

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/

like image 179
BenV Avatar answered Sep 20 '22 12:09

BenV


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/

like image 33
Loren Paulsen Avatar answered Sep 18 '22 12:09

Loren Paulsen