能否从Azure Function部署输出中获取Event Grid触发URL与密钥?
Absolutely! You can absolutely retrieve the Event Grid trigger URL and access key from your Azure Function's ARM deployment outputs in Azure DevOps (formerly VSTS) to use in your subsequent Event Grid subscription deployment. Here's how to make this happen smoothly:
First, you need to explicitly add output values to your ARM template that capture the trigger URL and key. This exposes the values so your pipeline can pick them up post-deployment.
Capture the full Event Grid trigger URL:
Use thelistCallbackUrlARM template function to fetch the complete trigger URL (which includes the access key by default). Here’s what the output block would look like:"outputs": { "eventGridTriggerUrl": { "type": "string", "value": "[listCallbackUrl(resourceId('Microsoft.Web/sites/functions', parameters('functionAppName'), parameters('eventGridFunctionName')), '2022-09-01').url]" } }Capture the trigger-specific access key (optional):
If you need the key separately (e.g., for validation or other integrations), you can retrieve the function's default key using thelistKeysfunction:"outputs": { "eventGridTriggerKey": { "type": "string", "value": "[listKeys(resourceId('Microsoft.Web/sites/functions', parameters('functionAppName'), parameters('eventGridFunctionName')), '2022-09-01').functionKeys.default]" } }Note: The
listCallbackUrlresult already includes the key, so you may not need this separate output unless you have a specific use case.
Once your ARM template deployment runs in your release pipeline, you need to tell Azure DevOps to save these outputs as pipeline variables:
- Open your Azure DevOps release pipeline and navigate to the ARM Template Deployment task.
- Scroll to the Outputs section and check the box labeled Publish deployment outputs to pipeline.
- Each output from your template will be available as a pipeline variable using the format
$(<DeploymentTaskName>.outputs['<OutputName>']). For example, if your deployment task is namedDeployFunctionAppand your output iseventGridTriggerUrl, the variable becomes$(DeployFunctionApp.outputs['eventGridTriggerUrl']).
Now you can reference these variables in the task that deploys your Event Grid subscription—here are two common approaches:
Using another ARM template for Event Grid
Pass the trigger URL as a parameter to your Event Grid subscription template:
"parameters": { "functionEndpointUrl": { "type": "string", "metadata": { "description": "The endpoint URL for the Azure Function Event Grid trigger" } } }, "resources": [ { "type": "Microsoft.EventGrid/systemTopics/eventSubscriptions", "name": "[concat(parameters('systemTopicName'), '/', parameters('eventSubscriptionName'))]", "apiVersion": "2022-06-15", "properties": { "destination": { "endpointType": "AzureFunction", "properties": { "resourceId": "[parameters('functionAppResourceId')]", "endpointUrl": "[parameters('functionEndpointUrl')]" } }, "filter": { "includedEventTypes": ["Microsoft.Storage.BlobCreated"] } } } ]
In the Azure DevOps task for this template, map the pipeline variable you captured earlier to the functionEndpointUrl parameter.
Using Azure CLI or PowerShell
If you prefer scripting, reference the variable directly in your CLI/PowerShell command:
az eventgrid event-subscription create \ --name "myStorageBlobSubscription" \ --source-resource-id "/subscriptions/<your-sub-id>/resourceGroups/<your-rg>/providers/Microsoft.Storage/storageAccounts/<your-storage-account>" \ --endpoint "$(DeployFunctionApp.outputs['eventGridTriggerUrl'])"
- Ensure the service principal used by your Azure DevOps pipeline has the Microsoft.Web/sites/functions/listkeys/action permission on the function app—this is required to fetch the trigger URL and keys.
- Use a compatible API version in your ARM template (the examples use
2022-09-01for functions; adjust to the latest version that works with your resources). - Test the pipeline variables first to confirm they’re populated correctly—you can add a PowerShell task to echo the variables and verify their values before deploying the Event Grid subscription.
内容的提问来源于stack exchange,提问作者Agrawal Shraddha

