Event Grid のイベントのフィルター処理

この記事では、Event Grid サブスクリプションを作成するときにイベントをフィルター処理する方法について説明します。 イベントのフィルター処理のオプションの詳細については、「Understand event filtering for Event Grid subscriptions (Event Grid サブスクリプションのイベントのフィルター処理について)」を参照してください。

イベントの種類でフィルター処理する

Event Grid サブスクリプションを作成するときに、エンドポイントに送信するイベントの種類を指定できます。 このセクションの例では、リソース グループのイベント サブスクリプションを作成しますが、送信されるイベントを Microsoft.Resources.ResourceWriteFailureMicrosoft.Resources.ResourceWriteSuccess に限定しています。 イベントの種類でイベントをフィルター処理する際にさらに柔軟性が必要な場合は、「演算子とデータでフィルター処理する」を参照してください。

Azure PowerShell

PowerShell の場合は、サブスクリプションの作成時に -IncludedEventType パラメーターを使用します。

$includedEventTypes = "Microsoft.Resources.ResourceWriteFailure", "Microsoft.Resources.ResourceWriteSuccess"

New-AzEventGridSubscription `
  -EventSubscriptionName demoSubToResourceGroup `
  -ResourceGroupName myResourceGroup `
  -Endpoint <endpoint-URL> `
  -IncludedEventType $includedEventTypes

Azure CLI

Azure CLI の場合は、--included-event-types パラメーターを使用します。 次の例では、Bash シェルで Azure CLI を使用しています。

includedEventTypes="Microsoft.Resources.ResourceWriteFailure Microsoft.Resources.ResourceWriteSuccess"

az eventgrid event-subscription create \
  --name demoSubToResourceGroup \
  --resource-group myResourceGroup \
  --endpoint <endpoint-URL> \
  --included-event-types $includedEventTypes

Azure Portal

システム トピックに対するイベント サブスクリプションを作成するときに、ドロップダウン リストを使用して、次の図に示すようにイベントの種類を選択します。

Screenshot showing the Create Subscription page with event types selected.

システム トピックに対する既存のサブスクリプションの場合は、次の図に示すように、[Event Subscription] (イベント サブスクリプション) ページの [フィルター] タブを使用します。

Screenshot showing the Event Subscription page with the Filters tab selected.

カスタム トピックの作成時にフィルターを指定するには、次の図に示すように、[イベントの種類の追加] リンクを選択します。

Screenshot showing the Create Event Subscription page for a custom topic.

カスタム トピックに対する既存のサブスクリプションのフィルターを指定するには、[Event Subscription] (イベント サブスクリプション) ページの [フィルター] タブを使用します。

Screenshot of the Event Subscription page with Add Event Type button selected.

Azure Resource Manager テンプレート

Resource Manager テンプレートの場合は、includedEventTypes プロパティを使用します。

"resources": [
  {
    "type": "Microsoft.EventGrid/eventSubscriptions",
    "name": "[parameters('eventSubName')]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectBeginsWith": "",
        "subjectEndsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [
          "Microsoft.Resources.ResourceWriteFailure",
          "Microsoft.Resources.ResourceWriteSuccess"
        ]
      }
    }
  }
]

注意

これらのフィルター (イベントの種類、件名、および高度) の詳細については、「Event Grid サブスクリプションでのイベントのフィルター処理を理解する」を参照してください。

件名でフィルター処理する

イベント データの件名でイベントをフィルター処理できます。 件名の先頭または末尾で照合する値を指定できます。 件名でイベントをフィルター処理する際にさらに柔軟性が必要な場合は、「演算子とデータでフィルター処理する」を参照してください。

次の PowerShell の例では、件名の先頭でフィルター処理するイベント サブスクリプションを作成しています。 -SubjectBeginsWith パラメーターを使用して、イベントを特定のリソースのイベントに限定しています。 ネットワーク セキュリティ グループのリソース ID を渡します。

Azure PowerShell

$resourceId = (Get-AzResource -ResourceName demoSecurityGroup -ResourceGroupName myResourceGroup).ResourceId

New-AzEventGridSubscription `
  -Endpoint <endpoint-URL> `
  -EventSubscriptionName demoSubscriptionToResourceGroup `
  -ResourceGroupName myResourceGroup `
  -SubjectBeginsWith $resourceId

次の PowerShell の例では、BLOB ストレージのサブスクリプションを作成しています。 ここでは、イベントは、件名の末尾が .jpg のイベントに限定されています。

$storageId = (Get-AzStorageAccount -ResourceGroupName myResourceGroup -AccountName $storageName).Id

New-AzEventGridSubscription `
  -EventSubscriptionName demoSubToStorage `
  -Endpoint <endpoint-URL> `
  -ResourceId $storageId `
  -SubjectEndsWith ".jpg"

Azure CLI

次の Azure CLI の例では、件名の先頭でフィルター処理するイベント サブスクリプションを作成しています。 --subject-begins-with パラメーターを使用して、イベントを特定のリソースのイベントに限定しています。 ネットワーク セキュリティ グループのリソース ID を渡します。

resourceId=$(az network nsg show -g myResourceGroup -n demoSecurityGroup --query id --output tsv)

az eventgrid event-subscription create \
  --name demoSubscriptionToResourceGroup \
  --resource-group myResourceGroup \
  --endpoint <endpoint-URL> \
  --subject-begins-with $resourceId

次の Azure CLI の例では、BLOB ストレージのサブスクリプションを作成しています。 ここでは、イベントは、件名の末尾が .jpg のイベントに限定されています。

storageid=$(az storage account show --name $storageName --resource-group myResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --resource-id $storageid \
  --name demoSubToStorage \
  --endpoint <endpoint-URL> \
  --subject-ends-with ".jpg"

Azure Portal

既存のイベント サブスクリプションの場合:

  1. [イベント サブスクリプション] ページで、 [サブジェクト フィルタリングを有効にする] を選択します。

  2. [次で始まるサブジェクト][次で終わるサブジェクト] のフィールドの 1 つ以上に値を入力します。 次のオプションでは、両方のオプションが選択されています。

    Screenshot of Event Subscription page with subject filtering example.

  3. 指定したフィルターの大文字と小文字とイベントの件名が一致する必要がある場合は、 [大文字小文字を区別した件名の一致] オプションを選択します。

イベント サブスクリプションの作成時に、作成ウィザードの [フィルター] タブを使用します。

Screenshot of Create Event Subscription page with the Filters tab selected.

Azure Resource Manager テンプレート

次の Resource Manager テンプレートの例では、件名の先頭でフィルター処理するイベント サブスクリプションを作成しています。 subjectBeginsWith プロパティを使用して、イベントを特定のリソースのイベントに限定しています。 ネットワーク セキュリティ グループのリソース ID を渡します。

"resources": [
  {
    "type": "Microsoft.EventGrid/eventSubscriptions",
    "name": "[parameters('eventSubName')]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectBeginsWith": "[resourceId('Microsoft.Network/networkSecurityGroups','demoSecurityGroup')]",
        "subjectEndsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [ "All" ]
      }
    }
  }
]

次の Resource Manager テンプレートの例では、BLOB ストレージのサブスクリプションを作成しています。 ここでは、イベントは、件名の末尾が .jpg のイベントに限定されています。

"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts/providers/eventSubscriptions",
    "name": "[concat(parameters('storageName'), '/Microsoft.EventGrid/', parameters('eventSubName'))]",
    "apiVersion": "2018-09-15-preview",
    "properties": {
      "destination": {
        "endpointType": "WebHook",
        "properties": {
          "endpointUrl": "[parameters('endpoint')]"
        }
      },
      "filter": {
        "subjectEndsWith": ".jpg",
        "subjectBeginsWith": "",
        "isSubjectCaseSensitive": false,
        "includedEventTypes": [ "All" ]
      }
    }
  }
]

注意

これらのフィルター (イベントの種類、件名、および高度) の詳細については、「Event Grid サブスクリプションでのイベントのフィルター処理を理解する」を参照してください。

演算子とデータでフィルター処理する

フィルター処理の柔軟性を高めるために、演算子とデータ プロパティを使用してイベントをフィルター処理することもできます。

高度なフィルターを使用してサブスクライブする

高度なフィルター処理に使用できる演算子とキーについては、「Advanced filtering (高度なフィルター処理)」を参照してください。

以下の例では、カスタム トピックを作成します。 これらの例では、カスタム トピックをサブスクライブし、データ オブジェクト内の値でフィルター処理を行います。 color プロパティが blue、red、または green に設定されているイベントがサブスクリプションに送信されます。

Azure PowerShell

PowerShell では、次を使用します。

$topicName = <your-topic-name>
$endpointURL = <endpoint-URL>

New-AzResourceGroup -Name gridResourceGroup -Location eastus2
New-AzEventGridTopic -ResourceGroupName gridResourceGroup -Location eastus2 -Name $topicName

$topicid = (Get-AzEventGridTopic -ResourceGroupName gridResourceGroup -Name $topicName).Id

$expDate = '<mm/dd/yyyy hh:mm:ss>' | Get-Date
$AdvFilter1=@{operator="StringIn"; key="Data.color"; Values=@('blue', 'red', 'green')}

New-AzEventGridSubscription `
  -ResourceId $topicid `
  -EventSubscriptionName <event_subscription_name> `
  -Endpoint $endpointURL `
  -ExpirationDate $expDate `
  -AdvancedFilter @($AdvFilter1)

Azure CLI

Azure CLI では、次を使用します。

topicName=<your-topic-name>
endpointURL=<endpoint-URL>

az group create -n gridResourceGroup -l eastus2
az eventgrid topic create --name $topicName -l eastus2 -g gridResourceGroup

topicid=$(az eventgrid topic show --name $topicName -g gridResourceGroup --query id --output tsv)

az eventgrid event-subscription create \
  --source-resource-id $topicid \
  -n demoAdvancedSub \
  --advanced-filter data.color stringin blue red green \
  --endpoint $endpointURL \
  --expiration-date "<yyyy-mm-dd>"

サブスクリプションに有効期限の日付が設定されていることに注意してください。

Azure portal

  1. [イベント サブスクリプション] ページで、 [高度なフィルター] セクションの [新しいフィルターの追加] を選択します。

    Screenshot showing the Event Subscription page with Add new filter link highlighted.

  2. キー、演算子、および比較する 1 つ以上の値を指定します。 次の例では、data.color はキーとして、String is in (次に含まれる文字列) は演算子として使用され、値に対して blueredgreen の値が指定されています。

    Screenshot showing an example of an advanced filter.

    Note

    高度なフィルターの詳細については、「Event Grid サブスクリプションでのイベントのフィルター処理を理解する」を参照してください。

フィルターをテストする

フィルターをテストするには、color フィールドが green に設定されているイベントを送信します。 green はフィルターに含まれている値の 1 つなので、このイベントはエンドポイントに配信されます。

Azure PowerShell

PowerShell では、次を使用します。

$endpoint = (Get-AzEventGridTopic -ResourceGroupName gridResourceGroup -Name $topicName).Endpoint
$keys = Get-AzEventGridTopicKey -ResourceGroupName gridResourceGroup -Name $topicName

$eventID = Get-Random 99999
$eventDate = Get-Date -Format s

$htbody = @{
    id= $eventID
    eventType="recordInserted"
    subject="myapp/vehicles/cars"
    eventTime= $eventDate
    data= @{
        model="SUV"
        color="green"
    }
    dataVersion="1.0"
}

$body = "["+(ConvertTo-Json $htbody)+"]"

Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

イベントが送信されないシナリオをテストするには、color フィールドが yellow に設定されているイベントを送信します。 yellow はサブスクリプションで指定された値の 1 つではないため、イベントはサブスクリプションに配信されません。

$htbody = @{
    id= $eventID
    eventType="recordInserted"
    subject="myapp/vehicles/cars"
    eventTime= $eventDate
    data= @{
        model="SUV"
        color="yellow"
    }
    dataVersion="1.0"
}

$body = "["+(ConvertTo-Json $htbody)+"]"

Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}

Azure CLI

Azure CLI では、次を使用します。

topicEndpoint=$(az eventgrid topic show --name $topicName -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name $topicName -g gridResourceGroup --query "key1" --output tsv)

event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/cars", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "model": "SUV", "color": "green"},"dataVersion": "1.0"} ]'

curl -X POST -H "aeg-sas-key: $key" -d "$event" $topicEndpoint

イベントが送信されないシナリオをテストするには、color フィールドが yellow に設定されているイベントを送信します。 yellow はサブスクリプションで指定された値の 1 つではないため、イベントはサブスクリプションに配信されません。

Azure CLI では、次を使用します。

event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/cars", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "model": "SUV", "color": "yellow"},"dataVersion": "1.0"} ]'

curl -X POST -H "aeg-sas-key: $key" -d "$event" $topicEndpoint

次のステップ

フィルター (イベントの種類、件名、および高度) の詳細を理解するには、「Event Grid サブスクリプションでのイベントのフィルター処理を理解する」を参照します。