跳至内容

如何使用 EventBridge 和 Amazon SNS 来监控我的 EC2 Linux 实例安全组的更改?

2 分钟阅读
0

我为我的 Amazon Elastic Compute Cloud (Amazon EC2) Linux 实例设置了一个安全组。我想使用 Amazon EventBridge 和 Amazon Simple Notification Service (Amazon SNS) 来监控我的安全组的更改。

简短描述

要监控安全组的更改,请创建一条 EventBridge 规则,使其在应用程序调用 API 以修改安全组时运行。然后,为与规则匹配的事件配置 Amazon SNS 通知。

解决方法

**先决条件:**创建 AWS CloudTrail 跟踪以记录 API 调用。

创建并订阅 SNS 主题

创建 SNS 主题。然后,订阅该主题,并在 Protocol(协议)中选择 Email(电子邮件)。Amazon SNS 会向您发送订阅确认电子邮件。

创建 EventBridge 规则

配置 EventBridge 规则,然后完成以下步骤以配置规则模式:

  1. 选择 Pre-defined pattern by service(服务提供的预定义模式)。

  2. 对于 Service provider(服务提供商),选择 AWS

  3. 对于 Service name(服务名称),选择 EC2

  4. 对于 Event type(事件类型),选择 AWS API call via CloudTrail(通过 CloudTrail 进行的 AWS API 调用)。

  5. 选择 Specific operation(特定操作),然后逐一输入以下 API 调用:

    AuthorizeSecurityGroupIngressAuthorizeSecurityGroupEgress
    RevokeSecurityGroupIngress
    RevokeSecurityGroupEgress

    **注意:**输入每个 API 调用后,选择 Add(添加)。这些 API 调用用于添加或删除安全组规则。
    上述设置会创建以下事件模式:

    {  "source": [
        "aws.ec2"
      ],
      "detail-type": [
        "AWS API Call via CloudTrail"
      ],
      "detail": {
        "eventSource": [
          "ec2.amazonaws.com"
        ],
        "eventName": [
          "AuthorizeSecurityGroupIngress",
          "AuthorizeSecurityGroupEgress",
          "RevokeSecurityGroupIngress",
          "RevokeSecurityGroupEgress"
        ]
      }
    }
  6. Select targets(选择目标)下,从 Target(目标)列表中选择 SNS topic(SNS 主题)。

  7. 对于 Topic(主题),输入您创建的主题。

  8. (可选)默认情况下,在 Configure input(配置输入)中的 Matched event(匹配事件)下,Matched event(匹配事件)处于选中状态。此配置会将事件的整个 JSON 输出传递给 SNS 主题。要筛选事件信息,请选择 Input transformer(输入转换器)。使用输入转换器自定义事件中的文本,以创建可读消息。例如,在 Input Path(输入路径)中使用以下键值对:

    {"name":"$.detail.requestParameters.groupId","source":"$.detail.eventName","time":"$.time","value":"$.detail"}

    Input Template(输入模板)中,输入要在消息中显示的文本和变量。
    输入模板示例:

    "A source API call was made against the security group name on time with the below details"" value "
  9. 选择 Create(创建)。

AWS 官方已更新 1 年前