10
votes

I have deployed Azure function using Arm template. I need the Function Key & host Key of deployed Azure Function in Powershell. Currently I am trying to get the keys From Output Section of ARM template

 "outputs": {
"FunctionAppName": {
  "type": "string",
  "value": "[variables('functionAppName')]"
},
"Key": {
  "type": "string",
  "value": "[listKeys(resourceId('Microsoft.Web/sites', '[variables('functionAppName')]'),'2015-08-01').keys]"
}

}

I tried different combinations But it failing. Is there any way to retrieve keys in Powershell?

5
yes. It throws Bad Request as errorRohi_Dev_1.0

5 Answers

15
votes

I got it working by using the following:

    "outputs": {
    "FunctionAppName": {
        "type": "string",
        "value": "[parameters('functionName')]"
    },
    "Key": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').key]"
    },        
    "Url": {
        "type": "string",
        "value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', parameters('existingFunctionAppName'), parameters('functionName')),'2015-08-01').trigger_url]"
    }        
}

I couldn't find any examples either. But by using the above, a quickstart sample at GitHub and the documentation of resource functions along with a some trial and error, I got it to out.

Please note the variables/parameters and names have been changed.

8
votes

I could not get the accepted answer to work to retrieve the default host key. @4c74356b41's answer is very close. You can get the keys out using the code below. The default host key will be in Outputs.functionKeys.Value.functionKeys.default.Value.

  "outputs": {
    "functionKeys": {
      "type": "object",
      "value": "[listkeys(concat(resourceId('Microsoft.Web/sites', variables('functionAppName')), '/host/default'), '2018-11-01')]"
    }
  }
2
votes

Question doesn't seem to be answered as it was requesting to get the Function key from Powershell and not ARM templates. I'm using the script below to get the function key from Powershell in Azure DevOps.

$accountInfo = az account show
$accountInfoObject = $accountInfo | ConvertFrom-Json
$subscriptionId  = $accountInfoObject.id

$resourceGroup = "your-resource-group"
$functionName = "your-function-name"

$functionkeylist = az rest --method post --uri "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$functionName/host/default/listKeys?api-version=2018-11-01"
$keylistobject = $functionkeylist | ConvertFrom-Json
$functionKey = $keylistobject.functionKeys.default

Hope this helps.

0
votes

First of all, you have an error in your syntax:

  "value": "[listKeys(resourceId('Microsoft.Web/sites', variables('functionAppName')),'2015-08-01').keys]"

but that won't help, I don't think its implemented for Azure Functions, I'm not 100% sure on this, but my efforts to retrive the keys were futile

0
votes

So to get this working for the function specific key for MyHttpFunction in the app MyFunctionApp, I had to use the following in the Outputs section of the ARM template:

"MyHttpFunctionKey": {
    "type": "string",
    "value": "[listkeys(resourceId('Microsoft.Web/sites/functions', 'MyFunctionApp', 'MyHttpFunction'), '2019-08-01').default]"
}

If that is called from Powershell using New-AzResourceGroupDeployment with a parameter -OutVariable arm then the following Powershell command will print the key: $arm.Outputs.myHttpFunctionKey.Value