Reputation: 718
I'm trying to get a full list of function endpoints in my Azure function app from a Powershell script. I can get the list of functions from the management.azure.com API, but it just has the function name, like...
/subscriptions/ea4a3766-c3a8-4b9c-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyFunctionAppName/functions/FunctionName
But the function actually has an endpoint of (for instance) http://myfunctionapp.azurewebsites.net/api/allsources/{sourceName}
How can I get that endpoint name from the Azure management API from Powershell? It's displayed in the "Get Function URL" button on the portal, so I would imagine it has to be there somewhere.
EDIT: The suggested duplicate still doesn't provide the actual function endpoint. For instance, I have a function called CheckLock. Its endpoint per the "Get Function URL" button on the portal (and the one that I want) is: https://myfunctionapp.azurewebsites.net/api/account/lock/{id}?code=myfunctioncode
What I'm getting from the suggested duplicate is:
@{
name=CheckLock;
function_app_id=/subscriptions/ea4a3766-c3a8-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myresourcegroup/providers/Microsoft.Web/sites/myfunctionappname;
script_root_path_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/CheckLock/;
script_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/bin/Funcs.dll;
config_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/CheckLock/function.json;
secrets_file_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/data/functions/secrets/CheckLock.json;
href=https://myfunctionappname.scm.azurewebsites.net/api/functions/CheckLock;
config=;
files=;
test_data=
}
Upvotes: 1
Views: 4555
Reputation: 1
Quick tip : if you can get azure context with Get-AzContext, you can get the access token to the header like this : (Get-AzContext).TokenCache[0].ReadItems().AccessToken
Upvotes: 0
Reputation: 12047
AZ PowerShell has introduced the Invoke-AzResourceAction
cmdlet. This does exactly what it says on the tin - it allows you to invoke an action against an Azure Resource.
It's possible to get a Function Key and URL through the listkeys
action as follows -
$functionApp = Get-AzWebAppSlot -Name $FunctionAppName -ResourceGroup $ResourceGroupName -Slot $Slot
$functionSecrets = Invoke-AzResourceAction -ResourceId ("{0}/functions/{1}" -f $functionApp.Id, $FunctionName) -Action "listkeys" -ApiVersion "2019-08-01" -Force
The variable $functionSecrets
now contains two properties -
key
: The default function-level key (will only work for $FunctionName
).trigger_url
: The URL to trigger $FunctionName
. Note that this property does not honour custom routes.Upvotes: 1
Reputation: 718
I got it. In the data for the function itself, the route is part of the properties.config object.
Request should look like this: https://management.azure.com/subscriptions/{subscriptionid}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}/functions/{functionName}?api-version=2016-08-01
In the return value is a properties
object, and within that is config
object. Underneath that is the route
property which contains the trigger endpoint.
In Powershell, it's this:
$functionData = Invoke-RestMethod -Method Get -Uri $functionName -Headers $accessTokenHeader
$triggerUrl = "https://$functionAppName.azurewebsites.net/api/" + $functionData.properties.config.route
You can test it here: https://learn.microsoft.com/en-us/rest/api/appservice/webapps/getfunction
Hope this helps someone else! Thanks to those who contributed.
Upvotes: 1
Reputation: 42043
The url you can get via Get Function URL
named trigger_url
, seems you could not be able to get all of them in a function app, but you can get it for one specific function, See : Web Apps - List Function Secrets, the trigger_url
in the response body is what you want.
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}/listsecrets?api-version=2016-08-01
Response body:
{
"key": "xxxxxxxxxxxxxxxxxxx",
"trigger_url": "https://xxxxx.azurewebsites.net/api/HttpTrigger1?code=xxxxxxxxxxxxxxx"
}
Note: If you get an error like below, you need to add an application setting AzureWebJobsSecretStorageType : Files
for your function app.
{
"error": {
"code": "Conflict",
"message": "System.InvalidOperationException: Runtime keys are stored on blob storage. This API doesn't support this configuration. Please change Environment variable AzureWebJobsSecretStorageType value to 'Files'. For more info, visit https://aka.ms/funcsecrets\r\n at Kudu.Core.Functions.FunctionManager.<GetKeyObjectFromFile>d__9`1.MoveNext() in C:\\Kudu Files\\Private\\src\\master\\Kudu.Core\\Functions\\FunctionManager.cs:line 141\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Kudu.Core.Functions.FunctionManager.<GetFunctionSecretsAsync>d__12.MoveNext() in C:\\Kudu Files\\Private\\src\\master\\Kudu.Core\\Functions\\FunctionManager.cs:line 220\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Kudu.Services.Functions.FunctionController.<GetSecrets>d__12.MoveNext() in C:\\Kudu Files\\Private\\src\\master\\Kudu.Services\\Functions\\FunctionController.cs:line 141"
}
}
Upvotes: 0