範例:整合來自 Datadog 和 Splunk 的通知 - AWS 事件偵測與回應使用者指南

範例:整合來自 Datadog 和 Splunk 的通知

此範例提供將來自 Datadog 和 Splunk 的通知整合到 AWS 事件偵測與回應的詳細步驟。

步驟 1:在 Amazon EventBridge 中將 APM 設定為事件來源

在您的 AWS 帳戶中,將每個 APM 設定為 Amazon EventBridge 中的事件來源。如需將 APM 設定為事件來源的指示,請參閱 Amazon EventBridge 合作夥伴中工具的事件來源設定指示

透過將 APM 設定為事件來源,您就可以從 APM 將通知擷取至 AWS 帳戶中的事件匯流排。設定完成後,AWS 事件偵測與回應就可以在事件匯流排收到事件時,啟動事件管理程序。此程序會將 Amazon EventBridge 新增為 APM 中的目的地。

步驟 2:建立自訂事件匯流排

最佳實務是使用自訂事件匯流排。AWS 事件偵測與回應使用自訂事件匯流排來擷取轉換的事件。AWS Lambda 函式會轉換合作夥伴事件匯流排事件,並將其傳送至自訂事件匯流排。AWS 事件偵測與回應會安裝受管規則,以從自訂事件匯流排擷取事件。

您可以使用預設事件匯流排,而不使用自訂事件匯流排。AWS 事件偵測與回應會通知受管規則從預設事件匯流排擷取事件,而非從自訂事件匯流排。

在 AWS 帳戶中建立自訂事件匯流排:
  1. 前往 https://console.aws.amazon.com/events 開啟 Amazon EventBridge 主控台

  2. 選擇匯流排事件匯流排

  3. 自訂事件匯流排下,選擇建立

  4. 名稱下提供事件匯流排的名稱。建議的格式為 APMName-AWSIncidentDetectionResponse-EventBus

    例如,如果您使用 Datadog 或 Splunk,請使用下列其中一項:

    • Datadog:Datadog-AWSIncidentDetectionResponse-EventBus

    • Splunk:Splunk-AWSIncidentDetectionResponse-EventBus

步驟 3:建立 AWS Lambda 函式來進行轉換

Lambda 函式會在步驟 1 的合作夥伴事件匯流排與步驟 2 的自訂 (或預設) 事件匯流排之間轉換事件。Lambda 函式轉換符合 AWS 事件偵測與回應受管規則。

在您的 AWS 帳戶中建立 AWS Lambda 函式
  1. 開啟 AWS Lambda 主控台的 Functions (函數) 頁面

  2. 選擇 Create function (建立函數)

  3. 選擇從頭開始撰寫索引標籤。

  4. 函式名稱中,使用 APMName-AWSIncidentDetectionResponse-LambdaFunction 格式輸入名稱。

    以下是 Datadog 和 Splunk 的範例:

    • Datadog:Datadog-AWSIncidentDetectionResponse-LambdaFunction

    • Splunk:Splunk-AWSIncidentDetectionResponse-LambdaFunction

  5. 執行時期中,輸入 Python 3.10

  6. 其餘欄位保留預設值。選擇 Create function (建立函數)

  7. 程式碼編輯頁面上,將預設 Lambda 函式內容取代為下列程式碼範例中的函式。

    請注意下列程式碼範例中以 # 開頭的註解。這些註解指出要變更哪些值。

    Datadog 轉換程式碼範本

    import logging import json import boto3 logger = logging.getLogger() logger.setLevel(logging.INFO) # Change the EventBusName to the custom event bus name you created previously or use your default event bus which is called 'default'. # Example 'Datadog-AWSIncidentDetectionResponse-EventBus' EventBusName = "Datadog-AWSIncidentDetectionResponse-EventBus" def lambda_handler(event, context): # Set the event["detail"]["incident-detection-response-identifier"] value to the name of your alert that is coming from your APM. Each APM is different and each unique alert will have a different name. # Replace the dictionary path, event["detail"]["meta"]["monitor"]["name"], with the path to your alert name based on your APM payload. # This example is for finding the alert name for Datadog. event["detail"]["incident-detection-response-identifier"] = event["detail"]["meta"]["monitor"]["name"] logger.info(f"We got: {json.dumps(event, indent=2)}") client = boto3.client('events') response = client.put_events( Entries=[ { 'Detail': json.dumps(event["detail"], indent=2), 'DetailType': 'ams.monitoring/generic-apm', # Do not modify. This DetailType value is required. 'Source': 'GenericAPMEvent', # Do not modify. This Source value is required. 'EventBusName': EventBusName # Do not modify. This variable is set at the top of this code as a global variable. Change the variable value for your eventbus name at the top of this code. } ] ) print(response['Entries'])

    Splunk 轉換程式碼範本

    import logging import json import boto3 logger = logging.getLogger() logger.setLevel(logging.INFO) # Change the EventBusName to the custom event bus name you created previously or use your default event bus which is called 'default'. # Example Splunk-AWSIncidentDetectionResponse-EventBus EventBusName = "Splunk-AWSIncidentDetectionResponse-EventBus" def lambda_handler(event, context): # Set the event["detail"]["incident-detection-response-identifier"] value to the name of your alert that is coming from your APM. Each APM is different and each unique alert will have a different name. # replace the dictionary path event["detail"]["ruleName"] with the path to your alert name based on your APM payload. # This example is for finding the alert name in Splunk. event["detail"]["incident-detection-response-identifier"] = event["detail"]["ruleName"] logger.info(f"We got: {json.dumps(event, indent=2)}") client = boto3.client('events') response = client.put_events( Entries=[ { 'Detail': json.dumps(event["detail"], indent=2), 'DetailType': 'ams.monitoring/generic-apm', # Do not modify. This DetailType value is required. 'Source': 'GenericAPMEvent', # Do not modify. This Source value is required. 'EventBusName': EventBusName # Do not modify. This variable is set at the top of this code as a global variable. Change the variable value for your eventbus name at the top of this code. } ] ) print(response['Entries'])
  8. 選擇部署

  9. PutEvents 許可新增至您要傳送轉換資料的目標事件匯流排的 Lambda 執行角色:

    1. 開啟 AWS Lambda 主控台的 Functions (函數) 頁面

    2. 選取函式,然後在組態索引標籤上選擇許可

    3. 執行角色下選取角色名稱,以在 AWS Identity and Access Management 主控台中開啟執行角色。

    4. 許可政策下,選取現有的政策名稱以開啟政策。

    5. 此政策中定義的許可下,選擇編輯

    6. 政策編輯器頁面上,選取新增陳述式

    7. 政策編輯器會新增類似下方新的空白陳述式

      IAM 主控台中 JSON 政策編輯器的螢幕擷取畫面。
    8. 將自動產生的新陳述式取代為下列內容:

      { "Sid": "AWSIncidentDetectionResponseEventBus0", "Effect": "Allow", "Action": "events:PutEvents", "Resource": "arn:aws:events:{region}:{accountId}:event-bus/{custom-eventbus-name}" }
    9. 如果您在 Lambda 程式碼中使用預設事件匯流排,則資源是您在 步驟 2:建立自訂事件匯流排 中建立的自訂事件匯流排的 ARN,或是預設事件匯流排的 ARN。

  10. 檢閱並確認已將所需許可新增至角色。

  11. 選擇將此新版本設定為預設值,然後選擇儲存變更

承載轉換需要什麼?

AWS 事件偵測與回應擷取的事件匯流排事件需要下列 JSON 金鑰:值對。

{ "detail-type": "ams.monitoring/generic-apm", "source": "GenericAPMEvent" "detail" : { "incident-detection-response-identifier": "Your alarm name from your APM", } }

下列範例顯示合作夥伴事件匯流排中轉換前和轉換後的事件。

{ "version": "0", "id": "a6150a80-601d-be41-1a1f-2c5527a99199", "detail-type": "Datadog Alert Notification", "source": "aws.partner/datadog.com/Datadog-aaa111bbbc", "account": "123456789012", "time": "2023-10-25T14:42:25Z", "region": "us-east-1", "resources": [], "detail": { "alert_type": "error", "event_type": "query_alert_monitor", "meta": { "monitor": { "id": 222222, "org_id": 3333333333, "type": "query alert", "name": "UnHealthyHostCount", "message": "@awseventbridge-Datadog-aaa111bbbc", "query": "max(last_5m):avg:aws.applicationelb.un_healthy_host_count{aws_account:123456789012} \u003c\u003d 1", "created_at": 1686884769000, "modified": 1698244915000, "options": { "thresholds": { "critical": 1.0 } }, }, "result": { "result_id": 7281010972796602670, "result_ts": 1698244878, "evaluation_ts": 1698244868, "scheduled_ts": 1698244938, "metadata": { "monitor_id": 222222, "metric": "aws.applicationelb.un_healthy_host_count" } }, "transition": { "trans_name": "Triggered", "trans_type": "alert" }, "states": { "source_state": "OK", "dest_state": "Alert" }, "duration": 0 }, "priority": "normal", "source_type_name": "Monitor Alert", "tags": [ "aws_account:123456789012", "monitor" ] } }

請注意,在事件轉換之前,detail-type 會指出警示來自哪個 APM、來源來自合作夥伴 APM,而且 incident-detection-response-identifier 索引鍵不存在。

Lambda 函式會轉換上述事件,並將其放入目標自訂或預設事件匯流排。轉換後的承載現在包含必要的鍵:值對。

{ "version": "0", "id": "7f5e0fc1-e917-2b5d-a299-50f4735f1283", "detail-type": "ams.monitoring/generic-apm", "source": "GenericAPMEvent", "account": "123456789012", "time": "2023-10-25T14:42:25Z", "region": "us-east-1", "resources": [], "detail": { "incident-detection-response-identifier": "UnHealthyHostCount", "alert_type": "error", "event_type": "query_alert_monitor", "meta": { "monitor": { "id": 222222, "org_id": 3333333333, "type": "query alert", "name": "UnHealthyHostCount", "message": "@awseventbridge-Datadog-aaa111bbbc", "query": "max(last_5m):avg:aws.applicationelb.un_healthy_host_count{aws_account:123456789012} \u003c\u003d 1", "created_at": 1686884769000, "modified": 1698244915000, "options": { "thresholds": { "critical": 1.0 } }, }, "result": { "result_id": 7281010972796602670, "result_ts": 1698244878, "evaluation_ts": 1698244868, "scheduled_ts": 1698244938, "metadata": { "monitor_id": 222222, "metric": "aws.applicationelb.un_healthy_host_count" } }, "transition": { "trans_name": "Triggered", "trans_type": "alert" }, "states": { "source_state": "OK", "dest_state": "Alert" }, "duration": 0 }, "priority": "normal", "source_type_name": "Monitor Alert", "tags": [ "aws_account:123456789012", "monitor" ] } }

請注意,detail-type 現在是 ams.monitoring/generic-apm,來源現在是 GenericAPMEvent,而詳細資訊下有新的鍵:值對:incident-detection-response-identifier

在上述範例中,incident-detection-response-identifier 值採用路徑 $.detail.meta.monitor.name 下的警示名稱。APM 彼此之間的 APM 警示名稱路徑不同。您必須修改 Lambda 函式,以便從正確的合作夥伴事件 JSON 路徑取得警示名稱,並將用其作為 incident-detection-response-identifier 值。

incident-detection-response-identifier 上設定的每個唯一名稱都會在上線期間提供給 AWS 事件偵測與回應團隊。若事件的 incident-detection-response-identifier 名稱不明,則不會處理。

步驟 4:建立自訂 Amazon EventBridge 規則

在步驟 1 中建立的合作夥伴事件匯流排需要您建立的 EventBridge 規則。此規則會將所需的事件從合作夥伴事件匯流排傳送至步驟 3 中建立的 Lambda 函式。

如需定義 EventBridge 規則的指引,請參閱 Amazon EventBridge 規則

  1. 前往 https://console.aws.amazon.com/events 開啟 Amazon EventBridge 主控台

  2. 選擇規則,然後選取與您的 APM 相關聯的合作夥伴事件匯流排。以下是合作夥伴事件匯流排的範例:

    • Datadog:aws.partner/datadog.com/eventbus-name

    • Splunk:aws.partner/signalfx.com/RandomString

  3. 選擇建立規則,以建立新的 EventBridge 規則。

  4. 針對規則名稱,以下列格式 APMName-AWS Incident Detection and Response-EventBridgeRule 輸入名稱,然後選擇下一步。範例名稱如下:

    • Datadog:Datadog-AWSIncidentDetectionResponse-EventBridgeRule

    • Splunk:Splunk-AWSIncidentDetectionResponse-EventBridgeRule

  5. 針對事件來源,選取 AWS 事件或 EventBridge 合作夥伴事件

  6. 範例事件建立方法保留預設值。

  7. 針對事件模式,選擇如下:

    1. 事件來源:EventBridge 合作夥伴。

    2. 合作夥伴:選取您的 APM 合作夥伴。

    3. 事件類型:所有事件。

    以下是範例事件模式:

    範例 Datadog 事件模式

    Datadog 事件模式的範例。

    範例 Splunk 事件模式

    Splunk 事件模式的範例。
  8. 針對目標,選擇如下:

    1. 目標類型:AWS 服務

    2. 選取目標:選擇 Lambda 函式。

    3. 函式:您在步驟 2 中建立的 Lambda 函式名稱。

  9. 選擇下一步儲存規則