事件网格筛选订阅中所有Azure功能的事件

时间:2020-06-15 13:16:56

标签: azure-functions serverless azure-powershell azure-cli azure-eventgrid

我试图过滤事件网格中的事件,仅当我的订阅中的Azure函数发生更改(例如配置更改,代码更新或创建/删除新函数)时才触发。

我正在使用的PowerShell脚本如下:

# Provide an endpoint for handling the events. Must be formatted "https://your-endpoint-URL"
$myEndpoint = "https://myendpoint-function.azurewebsites.net"
$subscriptionId = "abcde-34df-4493-9477-notrealid980"

$eventSubscriptionName = "FunctionConfigChanges"

# Select the Azure subscription you want to subscribe to. You need this command only if the 
# current subscription is not the one you wish to subscribe to.
Set-AzContext -Subscription $subscriptionId

$includedEventTypes = "Microsoft.Resources.ResourceActionSuccess", "Microsoft.Resources.ResourceDeleteSuccess", "Microsoft.Resources.ResourceWriteSuccess"
$AdvancedFilters = @{operator="StringContains"; key="Subject"; Values=@("providers/Microsoft.Web/sites")}
New-AzEventGridSubscription -Endpoint $myEndpoint -EventSubscriptionName $eventSubscriptionName -IncludedEventType $includedEventTypes -AdvancedFilter $AdvancedFilters

这将过滤所有功能和网站(选中$AdvancedFilters)。有什么方法可以将事件仅过滤到Azure函数? 欢迎在Azure CLI,门户网站,Powershell或.net sdk中提供任何类型的解决方案帮助。

1 个答案:

答案 0 :(得分:1)

对于您的要求,可以使用以下属性,请注意,数据对象中包含 operationName action

  1. 创建函数

    "eventType":"Microsoft.Resources.ResourceWriteSuccess"
    "operationName":"Microsoft.Web/sites/functions/write"
    
  2. 删除功能

    "eventType":"Microsoft.Resources.ResourceDeleteSuccess"   
    "operationName":"Microsoft.Web/sites/functions/delete"
    
  3. 代码已更新(run.csx文件)

    "eventType":"Microsoft.Resources.ResourceWriteSuccess"
    "operationName":"Microsoft.Web/sites/hostruntime/vfs/run.csx/write"
    
  4. 配置已更改

    "eventType":"Microsoft.Resources.ResourceWriteSuccess"
    "operationName":"Microsoft.Web/sites/config/write"
    

    请注意,订阅主题 App Service (目前在预览中)的内容可以在以下内容中进行过滤 属性:

    "eventType":"Microsoft.Web.AppUpdated"
    "action":"ChangedAppSettings
    

订户可以从主题属性中找到功能应用程序(应用服务)的名称和特定功能。

以下示例显示了根据上述要求设置过滤属性:

"filter": {
  "subjectBeginsWith": "",
  "subjectEndsWith": "",
  "includedEventTypes": [
    "Microsoft.Resources.ResourceWriteSuccess",
    "Microsoft.Resources.ResourceDeleteSuccess"
  ],
"advancedFilters": [
  {
    "values": [
      "Microsoft.Web/sites/functions/write",
      "Microsoft.Web/sites/functions/delete",
      "Microsoft.Web/sites/hostruntime/vfs/run.csx/write",
      "Microsoft.Web/sites/config/write"
    ],
    "operatorType": "StringIn",
    "key": "Data.operationName"
  }
]

}