

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 集成 AWS DevOps 亚马逊代理 EventBridge
<a name="configuring-integrations-and-knowledge-integrating-devops-agent-into-event-driven-applications-using-amazon-eventbridge-index"></a>

您可以使用调查和缓解生命周期中发生的事件，将 AWS DevOps Agent 与事件驱动的应用程序集成。 AWS DevOps 当调查或缓解措施的状态发生变化 EventBridge 时，代理向亚马逊发送事件。然后，您可以创建 EventBridge 规则，根据这些事件采取行动。

例如，您可以创建执行以下操作的规则：
+ 调查完成后，调用 AWS Lambda 函数来处理调查结果。
+ 调查失败或超时时发送 Amazon SNS 通知。
+ 创建新调查时更新票务系统。
+ 缓解措施完成后，启动 AWS Step Functions 工作流程。

## 如何 EventBridge 路线 AWS DevOps 代理活动
<a name="how-eventbridge-routes-aws-devops-agent-events"></a>

AWS DevOps 代理将事件发送到 EventBridge 默认事件总线。 EventBridge 然后根据您创建的规则评估事件。当事件与规则的事件模式匹配时， EventBridge 将该事件发送到指定的目标。

下图显示了如何 EventBridge 路由 AWS DevOps 代理事件。

![Amazon 通过规则将事件从 AWS 源 EventBridge 路由到 AWS 服务等目标。](https://docs.aws.amazon.com/zh_cn/devopsagent/latest/userguide/images/eventbridge-integration-how-it-works.png)


1. AWS DevOps 当调查或缓解生命周期状态发生变化时，代理会将事件发送到 EventBridge 默认事件总线。

1. EventBridge 根据您创建的规则评估事件。

1. 如果事件与规则的事件模式相匹配，则 EventBridge 将该事件发送到规则中指定的目标。

## AWS DevOps 代理活动
<a name="aws-devops-agent-events"></a>

AWS DevOps 代理将以下事件发送到 EventBridge。所有事件都使用源`aws.aidevops`。

AWS DevOps 代理发出这些事件进行调查，包括调查的缓解阶段。其他类型的代理工作，例如事件预防评估和自定义代理调用，不会发出这些事件。

每个事件的`time`字段是上次更新调查或缓解措施的时间，而不是 EventBridge 收到事件的时间。没有值的字段将被省略`detail`而不是设置为`null`。

### 支持的调查事件
<a name="supported-investigation-events"></a>


| detail-type | 说明 | 
| --- | --- | 
| Investigation Created | 在代理空间中进行了调查。 | 
| Investigation Priority Updated | 调查的优先顺序已更改。 | 
| Investigation In Progress | 一项调查开始了积极分析。 | 
| Investigation Completed | 调查成功结束，得出结论。 | 
| Investigation Failed | 调查遇到错误，无法完成。 | 
| Investigation Timed Out | 调查超过了允许的最大持续时间。 | 
| Investigation Cancelled | 调查在完成前被取消。 | 
| Investigation Pending Triage | 在开始积极分析之前，调查正在等待分类。 | 
| Investigation Linked | 一项调查与相关事件或罚单有关。 | 
| Investigation Skipped | 由于调查符合技能中定义的跳过标准，因此跳过了调查。该事件不包括跳过调查的原因。 | 

### 支持的缓解事件
<a name="supported-mitigation-events"></a>


| detail-type | 说明 | 
| --- | --- | 
| Mitigation In Progress | 缓解行动已开始。 | 
| Mitigation Completed | 缓解行动成功完成。 | 
| Mitigation Failed | 缓解措施遇到错误，无法完成。 | 
| Mitigation Timed Out | 缓解措施超过了允许的最大持续时间。 | 
| Mitigation Cancelled | 缓解行动在完成前被取消。 | 

有关详细的字段描述和示例事件，请参阅[AWS DevOps 代理事件详细信息参考](integrating-devops-agent-into-event-driven-applications-using-amazon-eventbridge-devops-agent-events-detail-reference.md)。

## 创建匹配的事件模式 AWS DevOps 代理活动
<a name="creating-event-patterns-that-match-aws-devops-agent-events"></a>

EventBridge 规则使用事件模式来选择事件并将其路由到目标。事件模式与其处理的事件的结构相匹配。您可以创建事件模式以根据事件字段筛选 AWS DevOps 代理事件。

以下示例显示了常见用例的事件模式。

**匹配所有 AWS DevOps 代理赛事 **

以下事件模式与来自 AWS DevOps 代理的所有事件相匹配。

```
{
  "source": ["aws.aidevops"]
}
```

**仅匹配调查事件 **

以下事件模式使用前缀匹配来仅选择调查生命周期事件。

```
{
  "source": ["aws.aidevops"],
  "detail-type": [{"prefix": "Investigation"}]
}
```

**仅匹配完成和失败事件 **

以下事件模式与已完成或失败的调查和缓解措施的事件相匹配。

```
{
  "source": ["aws.aidevops"],
  "detail-type": [
    "Investigation Completed",
    "Investigation Failed",
    "Mitigation Completed",
    "Mitigation Failed"
  ]
}
```

**特定代理空间的匹配事件 **

以下事件模式匹配来自特定代理空间的事件。

```
{
  "source": ["aws.aidevops"],
  "detail": {
    "metadata": {
      "agent_space_id": ["your-agent-space-id"]
    }
  }
}
```

有关事件模式的更多信息，请参阅[亚马逊 EventBridge 用户指南](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)中的*亚马逊 EventBridge 事件模式*。

## 检索调查或缓解措施摘要
<a name="retrieving-an-investigation-or-mitigation-summary"></a>

`Completed`事件标识代理生成的摘要，但它不包含摘要文本。摘要存储为日记记录。要读取它，请使用从事件中提取的值调用该[ListJournalRecords](https://docs.aws.amazon.com/devopsagent/latest/APIReference/API_ListJournalRecords.html)操作。


| 活动带来的价值 | `ListJournalRecords`参数 | 
| --- | --- | 
| detail.metadata.agent\_space\_id | agentSpaceId（必需） | 
| detail.metadata.execution\_id | executionId（必需） | 
| 不在活动中 | recordType— investigation\_summary\_md 用于调查摘要或mitigation\_summary\_md缓解措施摘要 | 

`ListJournalRecords`返回[JournalRecord](https://docs.aws.amazon.com/devopsagent/latest/APIReference/API_JournalRecord.html)对象列表。一次执行可以有多个相同类型的摘要记录。选择与之`recordId`匹配的记录`detail.data.summary_record_id`，然后阅读该记录`content`。 AWS DevOps 代理没有按 ID 返回单个日记记录的操作。

在构建之前，请注意以下几点`summary_record_id`：
+ 仅当为执行编写摘要记录时，该字段才存在。没有它`Completed`的事件意味着没有可用于该执行的摘要，因此您必须处理缺失该摘要的情况。
+ `Failed`、`Timed Out`，而且`Cancelled`事件永远不包括它。
+ 要检索摘要，您需要拥有运行事件使用者的账户的`aidevops:ListJournalRecords`权限。

有关权限的更多信息，请参阅 [DevOps 代理 IAM 权限](aws-devops-agent-security-devops-agent-iam-permissions.md)。

**使用 AWS CLI 检索摘要 **

以下命令列出了执行的调查摘要记录：

```
aws devops-agent list-journal-records \
  --agent-space-id e5f6g7h8-9012-34ab-cdef-example00000 \
  --execution-id b2c3d4e5-6789-01ab-cdef-example22222 \
  --record-type investigation_summary_md
```

**在 AWS Lambda 中处理已完成的调查 **

以下 Python 函数显示了规则目标处理这些事件的一种方法。它是一个示例，不是必需的实现。 EventBridge 支持其他目标类型，你的目标决定如何处理一个事件。此示例读取已完成调查的摘要，而忽略了没有摘要的事件：

```
import boto3

devops_agent = boto3.client("devops-agent")

INVESTIGATION_SUMMARY = "investigation_summary_md"
MITIGATION_SUMMARY = "mitigation_summary_md"


def lambda_handler(event, context):
    detail = event["detail"]
    metadata = detail["metadata"]
    data = detail["data"]

    summary_record_id = data.get("summary_record_id")
    if not summary_record_id:
        # A completed task does not always produce a summary.
        return {"summary": None}

    record_type = (
        MITIGATION_SUMMARY
        if event["detail-type"].startswith("Mitigation")
        else INVESTIGATION_SUMMARY
    )

    paginator = devops_agent.get_paginator("list_journal_records")
    pages = paginator.paginate(
        agentSpaceId=metadata["agent_space_id"],
        executionId=metadata["execution_id"],
        recordType=record_type,
    )

    for page in pages:
        for record in page["records"]:
            if record["recordId"] == summary_record_id:
                return {"summary": record["content"]}

    return {"summary": None}
```

用于`detail-type`决定请求哪种记录类型。调查和缓解事件均报告`data.task_type`为`INVESTIGATION`，因此`task_type`无法区分它们。

## 亚马逊 EventBridge 权限
<a name="amazon-eventbridge-permissions"></a>

AWS DevOps 代理不需要额外的权限即可将事件传送到 EventBridge。这些事件会自动发送到默认事件总线。

根据您为 EventBridge 规则配置的目标，您可能需要添加特定权限。有关目标所需权限的更多信息，请参阅《亚马逊 EventBridge 用户指南》 EventBridge[*中的为亚马逊*](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-use-resource-based.html)使用基于资源的策略。

## 其他 EventBridge 资源
<a name="additional-eventbridge-resources"></a>

有关 EventBridge 概念和配置的更多信息，请参阅*亚马逊 EventBridge 用户指南中的以下主题*：
+ [EventBridge 活动巴士 ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-bus.html)
+ [EventBridge 事件 ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)
+ [EventBridge 事件模式 ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)
+ [EventBridge 规则 ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html)
+ [EventBridge 目标 ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html)