如何创建一条 EventBridge 规则,通过使用输入转换器使 Amazon SNS 通知变得易于理解?

3 分钟阅读
0

我想创建一条 Amazon EventBridge 规则来发送易于理解的自定义 Amazon Simple Notification Service(Amazon SNS)通知。

简述

要使 Amazon SNS 事件通知易于理解,请使用 EventBridge 中的输入转换器。输入转换器可以在将事件的文本发送到 EventBridge 规则的目标之前,对文本进行自定义。有关完整的教程,请参阅教程: 使用输入转换器自定义 EventBridge 向事件目标传递的内容。

例如,您可以将 SNS 主题配置为目标,以接收来自输入转换器的自定义事件文本。您还可以创建 EventBridge 规则来匹配 AWS CloudTrail 捕获的与安全组相关的 API 调用。该规则配置了与相关 API 调用匹配的事件模式

以下解决方案包括两个示例事件,您可以使用它们来创建自己的 EventBridge 规则,从而使 Amazon SNS 通知易于理解。

解决方案

Amazon EC2 状态变更示例事件

以下示例将 SNS 主题配置为目标,以接收来自输入转换器的自定义事件文本。在此示例中,该事件是 Amazon Elastic Compute Cloud(Amazon EC2)的状态变更。

以下是以 JSON 格式发布到 EventBridge 事件总线的示例事件:

{  
    "version": "0",  
    "id": "2ad63080-2526-d42c-4983-123456789",  
    "detail-type": "EC2 Instance State-change Notification",  
    "source": "aws.ec2",  
    "account": "0123456789",  
    "time": "2023-05-17T16:30:32Z",  
    "region": "us-east-1",  
    "resources": [  
        "arn:aws:ec2:us-east-1:0123456789:instance/i-12345abcdefg"  
    ],  
    "detail": {  
        "instance-id": "i-12345abcdefg",  
        "state": "running"  
    }  
}

输入转换器有两个部分:

  • 输入路径: 在这里,您可以定义变量,这些变量使用 JSON 路径来引用原始事件源中的值。
  • **模板:**您要传递给目标的信息的模板。

输入路径

要创建输入路径,请参阅输入转换示例。在本示例中,使用以下输入路径:

{  
"timestamp": "$.time",  
"instance": "$.detail.instance-id",  
"state": "$.detail.state"  
}

模板

对于此示例,请使用以下模板:

示例 1:

"Instance example-instance is in example-state state"

示例 2:

"At example-timestamp, Instance example-instance changed its state to example-state. "

前面的示例模板采用以下格式向目标发送消息:

示例 1:

"Instance i-12345abcdefg is in running state"

示例 2:

"At 2023-05-17T16:30:32Z, Instance i-12345abcdefg changed its state to running. "

配置 EventBridge 以使用该输入转换器

要将 EventBridge 配置为向 SNS 主题发送自定义消息,请完成以下步骤:

**注意:**这些说明遵循创建对事件做出反应的规则中的步骤。它们还提供了配置输入转换器的具体步骤。

1.为 Amazon EC2 状态更改事件定义 EventBridge 规则

2.构建事件模式。在创建方法部分方法中,选择自定义模式(JSON 编辑器)

3.在事件模式中,输入以下 Amazon EC2 状态更改事件的 JSON 示例:

{  
"source": ["aws.ec2"],  
"detail-type": ["EC2 Instance State-change Notification"]  
}

4.选择下一步

5.对于目标类型,选择 AWS 服务

6.对于选择目标,选择 SNS 主题。然后,从下拉列表中选择您的主题。

7.对于其他设置,在配置目标输入下拉列表中,选择输入转换器。然后,选择配置输入转换器

8.在目标输入转换器部分下的输入路径中,粘贴以下 JSON:

{  
"timestamp": "$.time",  
"instance": "$.detail.instance-id",  
"state": "$.detail.state"  
}

9.对于输入模板,请复制并粘贴以下字符串模板:

"Instance example-instance is in example-state state"  
"At example-timestamp, Instance example-instance changed its state to example-state. "

10.选择确认,然后选择下一步

11.(可选)为您的规则添加标签,然后选择下一步

12.查看规则的详细信息,然后选择创建规则

Amazon EC2 安全组 API 调用的示例事件

在以下示例事件中,使用虚拟私有云(VPC)的 CreateSecurityGroup 创建安全组。AWS CloudTrail 会捕获该事件,并使用以下 JSON 格式将该事件发布到 Amazon CloudWatch:

{
    "version": "0",
    "id": "41dff147-cfbc-492a-9a98-9dd00d082417",
    "detail-type": "AWS API Call via CloudTrail",
    "source": "aws.ec2",
    "account": "123456789012",
    "time": "2017-07-14T16:36:23Z",
    "region": "us-east-1",
    "resources": [],
    "detail": {
        "eventVersion": "1.05",
        "userIdentity": {
            "type": "Root",
            "principalId": "123456789012",
            "arn": "arn:aws:iam::123456789012:root",
            "accountId": "123456789012",
            "accessKeyId": "ASIAIRP4G1234567891Q",
            "userName": "iamuser",
            "sessionContext": {
                "attributes": {
                    "mfaAuthenticated": "false",
                    "creationDate": "2017-07-14T16:27:38Z"
                }
            }
        },
        "eventTime": "2017-07-14T16:36:23Z",
        "eventSource": "ec2.amazonaws.com",
        "eventName": "CreateSecurityGroup",
        "awsRegion": "us-east-1",
        "sourceIPAddress": "72.21.196.64",
        "userAgent": "console.ec2.amazonaws.com",
        "requestParameters": {
            "groupName": "test",
            "groupDescription": "test",
            "vpcId": "vpc-54a4232d"
        },
        "responseElements": {
            "_return": true,
            "groupId": "sg-82d531f3"
        },
        "requestID": "a4609e55-69ac-4a2d-8057-843dc8b4658b",
        "eventID": "7774b898-59c1-48a5-9c41-af1bcaa04612",
        "eventType": "AwsApiCall"
    }
}

如何创建易于理解的自定义通知的示例

使用以下示例以 JSON 格式创建来自 EventBridge 的易于理解的自定义通知。

1.为与安全组相关的 API 调用创建 EventBridge 规则。对于事件模式,选择构建自定义事件模式。为您的使用案例修改示例事件模式。请注意,示例通知是针对 CreateSecurityGroup 的。但是,以下示例事件模式包括所有与安全组相关的 API 调用操作:

{
  "source": [
    "aws.ec2"
  ],
  "detail-type": [
    "AWS API Call via CloudTrail"
  ],
  "detail": {
    "eventSource": [
      "ec2.amazonaws.com"
    ],
    "eventName": [
      "CreateSecurityGroup",
      "AuthorizeSecurityGroupEgress",
      "AuthorizeSecurityGroupIngress",
      "DeleteSecurityGroup",
      "RevokeSecurityGroupEgress",
      "RevokeSecurityGroupIngress",
      "DescribeStaleSecurityGroups",
      "DescribeSecurityGroups",
      "DescribeSecurityGroupReferences"
    ]
  }
}

2.对于目标类型,选择 AWS 服务

3.对于选择目标,选择 SNS 主题。然后,从下拉列表中选择您的主题。

4.对于其他设置,在配置目标输入下拉列表中,选择输入转换器。然后,选择配置输入转换器

5.在目标输入转换器部分下的输入路径中,粘贴以下 JSON。输入您要在通知中使用的任何键值对。根据您的使用案例修改以下示例输入路径:

{  
    "sgname": "$.detail.requestParameters.groupName",  
    "sourceip": "$.detail.sourceIPAddress",  
    "vpcid": "$.detail.requestParameters.vpcId",  
    "action": "$.detail.eventName",  
    "sgid": "$.detail.responseElements.groupId",  
    "time": "$.detail.eventTime",  
    "region": "$.detail.awsRegion",  
    "user": "$.detail.userIdentity.userName",  
    "reqid": "$.detail.requestID"  
}

6.对于输入模板,粘贴以下字符串模板。为输入路径中的值添加一个带有占位符的字符串。此字符串是您的通知中易于理解的文本。根据您的使用案例修改以下示例输入模板:

"User example-user  has executed API call example-action from source IP example-sourceip on security group example-sgid/example-sgname that belongs to vpc example-vpcid in region example-region at time(UTC) example-time. The Request ID is example-reqid."

7.选择确认,然后选择下一步

8.(可选)为您的规则添加标签,然后选择下一步

9.查看规则的详细信息,然后选择创建规则

故障排除

如果在保存带有换行符的模板时收到 Invalid InputTemplate 错误,请用双引号括起每一行。请参见以下示例:

"example-type example-arn has executed action API example-action on Security Group example-sgid located in AWS region example-region at example-time (UTC)."
"Event Details:"
"Request ID: example-requestid"
"Event ID: example-eventid"
"Event Source: example-eventsource"
"Event Type: example-eventtype"

如果您的规则运行但未能调用目标,则为 EventBridge 配置死信队列,以接收与输入转换器相关的错误。

EventBridge 在输入路径匹配中最多支持 100 个变量。要提取 10 个以上的变量,请将整个事件传递给一个 AWS Lambda 函数。编写此函数来解析您的变量,并按照发布所需的格式构造消息正文。然后,按照所需格式构建消息正文以发布到 Amazon SNS。有关更多信息,请参阅如何使用 Lambda 函数向 Amazon SNS 主题发布消息?

AWS 官方
AWS 官方已更新 6 个月前