1
votes

I have a http triggered azure function whose function key I need to populate during deployment itself. I am currently trying to get the function key using the process given here (only instead of calling the host key API I am calling the function key API /admin/functions/$functionName/keys?code=$masterKey). I am making a call to the API as soon as I upload the zipped function code to the function app using the KUDU zip API.

The issue I am facing is that while trying to set the application settings of the function app I am getting the error

Invoke-RestMethod : {"id":"643ea0b3-5ffe-4683-a8d3-62daec8c8db9","requestId":"ec5461d6-57bc-45dd-b3ac-358602cfa94c","statusCode":500,"errorCode":0,"message":" Value cannot be null.\r\nParameter name: source"}

But this works when I try it out locally. I am doubting that the function is not yet deployed when I make a REST call to get the function key and hence the error. What does the error message mean and how can I fix it?. Is it a transient issue?

UPDATE: code snippet

function get_credentials($resourceGroupName, $functionAppName){
$creds = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName `
-ResourceType Microsoft.Web/sites/config -ResourceName "$functionAppName/publishingcredentials" `
-Action list -ApiVersion 2015-08-01 -Force

$username = $creds.Properties.PublishingUserName
$password = $creds.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
return $base64AuthInfo
}
function get_master_key($kuduAuthToken, $functionAppName ){
    Write-Host "KUDU token is $kuduAuthToken"
    $apiUrl = "https://$functionAppName.scm.azurewebsites.net/api/functions/admin/masterkey"
    $result = Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization"="Basic $kuduAuthToken";"If-Match"="*"} 
    $masterKey = $result.masterKey
    Write-Host "master key is $masterKey"
    return $masterKey
}

function get_function_key($functionAppName, $functionName, $masterKey ){

    $apiUrl = "https://$functionAppName.azurewebsites.net/admin/functions/$functionName/keys?code=$masterKey"
    Write-Host "Calling $apiUrl"
    $result = Invoke-RestMethod -Uri $apiUrl 
    Write-Host "Result is $result"
    return $result.keys[0].value
    Write-Host "host token is $result"
}
$authToken = get_credentials $resourceGroupName $functionAppName
$masterKey = get_master_key $authToken $functionAppName
get_function_key $functionAppName $purgeFunctionName $masterKey
1

1 Answers

0
votes

According to your description, I tested your workflow on my side and found it only occurred once.

enter image description here

After that, the same request could work as expected.

enter image description here

Per my understanding, this may be a transient issue as you guessed. Also, please make sure your function could work as expected. Additionally, for generating each function key for your function instead of using the default host key, you could also leverage the ARM REST API (e.g https://{functionappname}.azurewebsites.net/admin/functions/{functionname}/keys/{keyname}) to create a new function key for your existing function.

enter image description here

Detailed commands about creating the function key, you could follow this similar issue.