6

I'm trying to build a connection string from a storage account used elsewhere in the template and I have

"StorageConnectionString": {
               "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',
 variables('storageName'),';AccountKey=',
 listKeys(resourceId('Microsoft.Storage/storageAccounts',
 variables('storageName')), providers('Microsoft.Storage',
 'storageAccounts').apiVersions[0]).key1)]",
              "type": "Custom"
             },

Which I found from ARM - How can I get the access key from a storage account to use in AppSettings later in the template? however the syntax in that question no longer appears to work. I get an error that key1 is not a property which is known. Apparently there is a property called keys but that is, as one might expect, a structure of some sort. I have been unable to figure out what the property of the primary key is from that structure. I've tried

  • key1 -> Template language expression property 'key1' doesn't exist, available properties are 'keys
  • keys -> The provided parameters for template language function 'concat' are invalid. Either all or none of the parameters must be an array.
  • keys.key1
  • keys.primaryKey
  • keys[0]

All of which have failed. I tried putting an output at the end of the file but outputting keys just seems to output no value.

Community
  • 1
  • 1
stimms
  • 42,945
  • 30
  • 96
  • 149

2 Answers2

10

As it turns out the structure of the object returned from listKeys is an array of keys which looks like

[
  { "keyName":"key1", "permissions":"Full", "value":"keyvalue1"},
  { "keyName":"key2", "permissions":"Full", "value":"keyvalue2"}
]

So the correct solution to getting the value out was to do keys[0].value.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
stimms
  • 42,945
  • 30
  • 96
  • 149
1

You should use the listKeys() function

https://azure.microsoft.com/en-us/documentation/articles/resource-group-template-functions/#listkeys

Neil Mackenzie
  • 2,817
  • 14
  • 11
  • I did use listKeys but there doesn't seem to be any documentation on what gets returned by that when used against azure storage. It seems to be some structure but I don't know what or how to get the textual representation of the key for use in concat. – stimms Mar 14 '16 at 04:08
  • 1
    It returns a based64 encoded string that represents the primary (or secondary) storage key for the account. If you take a look at this template: https://github.com/rjmax/ArmExamples/blob/master/listKeysSample.json and deploy that you can see the output. Will look something like this: 3mI6Qg6+TtZ3+aDp/AWlft78UuW7MLFOdR6pcpYHBj0/IQMWNRJU55QPB5bmNgVIIs7V79bNTrnlywujsTXqsw== – bmoore-msft Mar 14 '16 at 17:56
  • In point of fact, the listKeys function returns a "keys" object, not a base 64 string. That "keys" object is difficult to find documentation regarding. – Tom Padilla May 12 '20 at 12:52