Is there a option to get the event grid trigger url + key at output value from the deployment of a Azure Function?
The scenario we would like to do is as followed:
- We deploy a Function Service in a VSTS release via ARM.
- With the Function service deployed we deploy the event grid subscription.
Thanks,
Shraddha Agrawal
Yes, there is a way using the REST API to obtain a function access code. The following are the steps:
Let assume a name of the function is EventGridTrigger2 and the run.csx:
#r "Newtonsoft.Json"
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static void Run(JObject eventGridEvent, TraceWriter log)
{
log.Info(eventGridEvent.ToString(Formatting.Indented));
}
and the function.json file:
{
"bindings": [
{
"type": "eventGridTrigger",
"name": "eventGridEvent",
"direction": "in"
}
],
"disabled": false
}
As you can see the above bindings is untyped, which will work for any output schema such as InputEventSchema, EventGridSchema (default schema) and CloudEventV01Schema (after fixing some bug).
The destination property of the created Subscription looks like the following:
"destination": {
"properties": {
"endpointUrl": null,
"endpointBaseUrl": "https://myFunctionApp.azurewebsites.net/admin/extensions/EventGridExtensionConfig"
},
"endpointType": "WebHook"
},
Note, that the full subscriberUrl for Azure EventGrid trigger has the following format, where the query string contains parameters for routing request to the properly function:
https://{FunctionApp}.azurewebsites.net/admin/extensions/EventGridExtensionConfig?functionName={FunctionName}&code={masterKey}
For creating a subscriber we have to use its full subscriberUrl included a query string. In this moment, the only unknown value is the masterKey.
To obtain a Function App (Host) masterkey we have to use a management REST API call:
https://management.azure.com/subscriptions/{mySubscriptionId}/resourceGroups/{myResGroup}/providers/Microsoft.Web/sites/{myFunctionApp}/functions/admin/masterkey?api-version=2016-08-01
the response has the following format:
{
"masterKey": "*************************************************"
}
Note, that the authentication Bearer token is required for this call.
Once we have a masterKey for the FunctionApp (host), we can use it for any function within this host.
I think you are asking: "how can I deploy an Azure Function with a step in a VSTS Release using ARM and get its trigger url so that I can use the trigger url in the next VSTS Release step?"
It's not very well documented, but using the official docs, this blog post and some trial and error, we've figured out how.
This is what the ARM should look like:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {}
"variables": {},
"resources": [],
"outputs": {
"triggerUrl": {
"type": "string",
"value": "[listsecrets(resourceId('Microsoft.Web/sites/functions', 'functionAppName', 'functionName'),'2015-08-01').trigger_url]"
}
}
}
You deploy it with an "Azure Resource Group Deployment" step, make sure that you enter a variable name in the "Deployment outputs" text box, let say triggerUrl
.
Example output:
{"triggerUrl":{"type":"String","value":"https://functionAppName.azurewebsites.net/api/functionName?code=1234"}}
Then you put a PowerShell step (or an Azure PowerShell step) afterwards that picks up the value from the variable.
$environmentVariableName = "triggerUrl"
$outputVariables = (Get-Item env:$environmentVariableName).Value
Then do something with it.
With update of Functions App V2.0.12050 the URI of the Event-Grid trigger is a little different. See also here