dushyantp
dushyantp

Reputation: 4720

Event grid filter events to all Azure functions in subscription

I am trying to filter events in event grid to be triggered only when an Azure function changes in my subscription (say configuration change, code updated or new function created/deleted).

The PowerShell script I am using is as follows:

# 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

This filters to all functions and websites (check $AdvancedFilters). Is there any way to get the event to be filtered to Azure functions only? Any kind of solution help in Azure CLI, portal, Powershell or .net sdk is welcome.

Upvotes: 0

Views: 890

Answers (1)

Roman Kiss
Roman Kiss

Reputation: 8235

For your requirements the following properties can be used, notice that the operationName and action are in the data object:

  1. Creating function:

    "eventType":"Microsoft.Resources.ResourceWriteSuccess"
    "operationName":"Microsoft.Web/sites/functions/write"
    
  2. Deleting function:

    "eventType":"Microsoft.Resources.ResourceDeleteSuccess"   
    "operationName":"Microsoft.Web/sites/functions/delete"
    
  3. Code updated (run.csx file):

    "eventType":"Microsoft.Resources.ResourceWriteSuccess"
    "operationName":"Microsoft.Web/sites/hostruntime/vfs/run.csx/write"
    
  4. Configuration changed:

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

    Note, that the subscribing on the topic App Service (presently in the preview) we can filtered on the following properties:

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

The subscriber can found the name of the function app (App Service) and specific function from the subject property.

The following example shows setup the filtering properties based on the above requirements:

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

}

Upvotes: 2

Related Questions