After updates to Microsoft's ARM APIs it is now possible to retrieve Azure Function keys directly from the ARM deployment outputs.
Example
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appServiceName": {
"type": "string"
}
},
"variables": {
"appServiceId": "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]"
},
//... implementation omitted
"outputs": {
"functionKeys": {
"type": "object",
"value": "[listkeys(concat(variables('appServiceId'), '/host/default'), '2018-11-01')]"
}
}
}
Outputs
The Outputs property will contain a Newtonsoft.Json.Linq.JObject
entry that contains all of the keys for the Azure Function i.e., master, system keys, and function keys (which includes the default key). Unfortunately, the JObject combined with the deployment variable type is a bit tortuous to get into and you should be warned, is case sensitive. (If you're working in PowerShell it can be massaged into hashtables
for consumption. See Bonus below.)
$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
Bonus
The code below gets rid of the extra .Value
calls.
function Convert-OutputsToHashtable {
param (
[ValidateNotNull()]
[object]$Outputs
)
$Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
$ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
} else {
$ht[$_.Key] = $_.Value.Value
}
} { $ht }
}