2

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

4

3 回答 3

2

是的,有一种方法可以使用 REST API 来获取函数访问代码。以下是步骤:

  1. 假设函数的名称是EventGridTrigger2和 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));
    
    }
    

和 function.json 文件:

    {
        "bindings": [
        {
          "type": "eventGridTrigger",
          "name": "eventGridEvent",
          "direction": "in"
        }
       ],
       "disabled": false
    }

如您所见,上述绑定是无类型的,它适用于任何输出模式,例如InputEventSchemaEventGridSchema(默认模式)和CloudEventV01Schema(修复了一些错误之后)。

  1. 创建的订阅的目标属性如下所示:

    "destination": {
        "properties": {
          "endpointUrl": null,
          "endpointBaseUrl": "https://myFunctionApp.azurewebsites.net/admin/extensions/EventGridExtensionConfig"
        },
        "endpointType": "WebHook"
      },
    

请注意,Azure EventGrid 触发器的完整subscriberUrl 具有以下格式,其中查询字符串包含用于将请求路由到正确函数的参数:

https://{FunctionApp}.azurewebsites.net/admin/extensions/EventGridExtensionConfig?functionName={FunctionName}&code={masterKey}

为了创建一个订阅者,我们必须使用它的完整订阅者Url 包含一个查询字符串。此时此刻,唯一未知的值就是masterKey。

  1. 要获取功能应用程序(主机)主密钥,我们必须使用管理 REST API 调用:

    https://management.azure.com/subscriptions/{mySubscriptionId}/resourceGroups/{myResGroup}/providers/Microsoft.Web/sites/{myFunctionApp}/functions/admin/masterkey?api-version=2016-08-01
    

响应具有以下格式:

    {
       "masterKey": "*************************************************"
    }

请注意,此调用需要身份验证承载令牌。

一旦我们有了 FunctionApp(主机)的主密钥,我们就可以将它用于该主机中的任何功能。

于 2018-05-16T18:05:22.357 回答
1

我想您是在问:“如何使用 ARM 在 VSTS 发布中部署一个 Azure 函数并获取其触发 URL,以便我可以在下一个 VSTS 发布步骤中使用触发 URL?”

它没有很好的文档记录,但是使用官方文档这篇博客文章和一些试验和错误,我们已经弄清楚了如何做。

这就是 ARM 的样子:

{
  "$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]"
    }
  }
}

您使用“Azure 资源组部署”步骤部署它,确保在“部署输出”文本框中输入变量名称,比如说triggerUrl.

示例输出:

{"triggerUrl":{"type":"String","value":"https://functionAppName.azurewebsites.net/api/functionName?code=1234"}}

然后,您放置一个 PowerShell 步骤(或 Azure PowerShell 步骤),从变量中获取值。

$environmentVariableName = "triggerUrl"
$outputVariables = (Get-Item env:$environmentVariableName).Value

然后用它做点什么。

于 2018-05-17T20:53:33.040 回答
0

随着 Functions App V2.0.12050的更新,Event-Grid 触发器的 URI 略有不同。另请参阅此处

于 2018-10-01T08:51:25.200 回答