Event Grid에 대한 이벤트 필터링

이 문서에서는 Event Grid 구독을 만들 때 이벤트를 필터링하는 방법을 보여줍니다. 이벤트 필터링에 대한 옵션을 알아보려면 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.

시스템 토픽에 대한 기존 구독의 경우 다음 이미지와 같이 이벤트 구독 페이지의 필터 탭을 사용합니다.

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

다음 이미지와 같이 이벤트 유형 추가 링크를 선택하여 사용자 지정 토픽을 만드는 동안 필터를 지정할 수 있습니다.

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

기존 구독에 대한 필터를 사용자 지정 토픽으로 지정하려면 이벤트 구독 페이지의 필터 탭을 사용합니다.

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 Storage에 대한 구독을 만듭니다. 이벤트를 .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 Storage에 대한 구독을 만듭니다. 이벤트를 .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. 제목 시작 문자제목 종료 문자 필드 중 하나 이상에 값을 입력합니다. 다음 옵션에서는 두 옵션을 모두 선택했습니다.

    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 Storage에 대한 구독을 만듭니다. 이벤트를 .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 구독에 대한 이벤트 필터링 이해를 참조하세요.

연산자 및 데이터별 필터링

보다 유연한 필터링을 원한다면 연산자 및 데이터 속성을 사용하여 이벤트를 필터링하면 됩니다.

고급 필터가 포함된 구독

고급 필터링에 사용할 수 있는 연산자 및 키에 대해 알아보려면 고급 필터링을 참조하세요.

다음은 사용자 지정 토픽을 만드는 예제입니다. 사용자 지정 토픽을 구독하고 데이터 개체의 값으로 필터링합니다. 색 속성이 파란색, 빨간색 또는 녹색으로 설정된 이벤트가 구독에 전송됩니다.

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. 키, 연산자 및 비교할 값을 지정합니다. 다음 예에서는 data.color가 키로 사용되고, 문자열이 다음에 있음이 연산자로 사용되고, blue, red, green 값이 값에 대해 지정됩니다.

    Screenshot showing an example of an advanced filter.

    참고 항목

    고급 필터에 대한 자세한 내용은 Event Grid 구독에 대한 이벤트 필터링 이해를 참조하세요.

필터 테스트

필터를 테스트하려면 색 필드가 녹색으로 설정된 이벤트를 보냅니다. 녹색은 필터의 값 중 하나이므로 이벤트가 엔드포인트로 전달됩니다.

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}

이벤트가 전송되지 않은 시나리오를 테스트하려면 색 필드가 노란색으로 설정된 이벤트를 보냅니다. 노란색은 구독에서 지정된 값 중 하나가 아니므로 이벤트는 구독에 배달되지 않습니다.

$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

이벤트가 전송되지 않은 시나리오를 테스트하려면 색 필드가 노란색으로 설정된 이벤트를 보냅니다. 노란색은 구독에서 지정된 값 중 하나가 아니므로 이벤트는 구독에 배달되지 않습니다.

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 구독에 대한 이벤트 필터링 이해를 참조하세요.