本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
示例:整合来自 Datadog 和 Splunk 的通知
此示例提供了将来自 Datadog 和 Splunk 的通知集成到 AWS 事件检测和响应的详细步骤。
主题
第 1 步:在 Amazon 中将您的 APM 设置为事件源 EventBridge
在您的 AWS 账户中 APMs ,将您的每一个都设置为 Amazon EventBridge 中的事件源。有关将 APM 设置为事件源的说明,请参阅 A mazon EventBridge 合作伙伴中针对您的工具的事件源设置说明
通过将您的 APM 设置为事件源,您可以将来自 APM 的通知提取到您的 AWS 账户中的事件总线。设置完成后,AWS 事件检测和响应可在事件总线收到事件时启动事件管理流程。此过程会将亚马逊添加 EventBridge 为您的 APM 中的目的地。
步骤 2:创建自定义事件总线
使用自定义事件总线是最佳实践。AWS 事件检测和响应使用自定义事件总线来摄取转换后的事件。 AWS Lambda 函数转换伙伴事件总线事件并将其发送到自定义事件总线。AWS 事件检测和响应会安装托管规则,用于从自定义事件总线提取事件。
您可以使用默认事件总线代替自定义事件总线。AWS 事件检测和响应修改托管规则,使其从默认事件总线而不是自定义事件总线中提取。
在您的 AWS 账户中创建自定义事件总线:
-
打开 Amazon EventBridge 控制台,网址为 https://console.aws.amazon.com/events/
-
选择 “巴士”、“活动总线”。
-
在 “自定义事件总线” 下,选择 “创建”。
-
在 “名称” 下为您的活动巴士提供一个名称。推荐的格式为 APMName-AWSIncidentDetectionResponse-EventBus。
例如,如果您使用 Datadog 或 Splunk,请使用以下方法之一:
-
Datadog: Datadog-AWSIncidentDetectionResponse-EventBus
-
Splunk: Splunk-AWSIncidentDetectionResponse-EventBus
-
步骤 3:创建用于转换的 AWS Lambda 函数
Lambda 函数在步骤 1 中的伙伴事件总线和步骤 2 中的自定义(或默认)事件总线之间转换事件。Lambda 函数转换与 AWS 事件检测和响应托管规则相匹配。
在您的 AWS 账户中创建 AWS Lambda 函数
-
在 AWS Lambda 控制台上打开 “函数” 页面
。 -
选择创建函数。
-
选择 “从头开始作者” 选项卡。
-
在函数名称中,使用格式输入名称
APMName-AWSIncidentDetectionResponse-LambdaFunction
。以下是 Datadog 和 Splunk 的示例:
Datadog: Datadog-AWSIncidentDetectionResponse-LambdaFunction
-
Splunk: Splunk-AWSIncidentDetectionResponse-LambdaFunction
-
对于运行时,输入 Python 3.10。
-
将其余字段保留为默认值。选择创建函数。
-
在代码编辑页面上,将默认 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'])
-
选择部署。
-
为要将转换后的数据发送到的事件总线的 Lambda 执行角色添加PutEvents权限:
在 AWS Lambda 控制台上打开 “函数” 页面
。 -
选择函数,然后在 “配置” 选项卡上选择 “权限”。
-
在 “执行角色” 下,选择角色名称以在 AWS Identity and Access Management 控制台中打开执行角色。
-
在 “权限策略” 下,选择现有策略名称以打开策略。
-
在此策略中定义的权限下,选择编辑。
-
在策略编辑器页面上,选择添加新声明:
-
策略编辑器添加了一个新的空白语句,类似于以下内容
-
将新的自动生成的语句替换为以下语句:
{ "Sid": "AWSIncidentDetectionResponseEventBus0", "Effect": "Allow", "Action": "events:PutEvents", "Resource": "arn:aws:events:{region}:{accountId}:event-bus/{custom-eventbus-name}" }
资源是您在中创建的自定义事件总线的 ARN,步骤 2:创建自定义事件总线或者如果您在 Lambda 代码中使用默认事件总线,则为默认事件总线的 ARN。
-
查看并确认所需的权限已添加到角色中。
-
选择 “将此新版本设为默认版本”,然后选择 “保存更改”。
有效载荷转换需要什么?
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
是 sourceams.monitoring/generic-apm
,现在是 sourceGenericAPMEvent
,详细信息下有新的 key: value pair:。incident-detection-response-identifier
在前面的示例中,该incident-detection-response-identifier
值取自路径下的警报名称$.detail.meta.monitor.name
。APM 警报名称路径从一个 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 规则。
-
打开 Amazon EventBridge 控制台,网址为 https://console.aws.amazon.com/events/
-
选择规则,然后选择与您的 APM 关联的合作伙伴事件总线。以下是合作伙伴活动总线的示例:
Datadog:aws。 partner/datadog.com/eventbus-名字
Splunk:aws。 partner/signalfx.com/RandomString
-
选择 “创建规则” 以创建新 EventBridge 规则。
-
在规则名称中,输入以下格式的名称
APMName-AWS Incident Detection and Response-EventBridgeRule
,然后选择下一步。以下是示例名称:-
Datadog: Datadog-AWSIncidentDetectionResponse-EventBridgeRule
-
Splunk: Splunk-AWSIncidentDetectionResponse-EventBridgeRule
-
-
对于事件来源,选择 AWS 事件或 EventBridge 合作伙伴活动。
-
将示例事件和创建方法保留为默认值。
-
对于事件模式,请选择以下选项:
事件来源: EventBridge 合作伙伴。
合作伙伴:选择您的 APM 合作伙伴。
事件类型:所有事件。
以下是事件模式示例:
Datadog 事件模式示例
Splunk 事件模式示例
-
对于 “目标”,请选择以下选项:
-
目标类型: AWS 服务
-
选择目标:选择 Lambda 函数。
-
函数:您在步骤 2 中创建的 Lambda 函数的名称。
-
-
选择下一步,保存规则。