如果使用 AWS Config 时 AWS 资源不合规,如何通知我?

2 分钟阅读
0

我创建了一个当 AWS 资源不合规时触发通知的 Amazon EventBridge 规则,但响应为 JSON 格式。如何才能收到含有自定义通知的电子邮件?

简短描述

使用包含自定义事件模式和输入转换器的 EventBridge 规则以匹配表示为 NON_COMPLIANT 的 AWS Config 评估规则输出。然后,将响应路由到某个 Amazon Simple Notification Service (Amazon SNS) 主题。

解决方法

在以下示例中,当 ec2-security-group-attached-to-eni 托管规则将 Amazon Elastic Compute Cloud (Amazon EC2) 安全组的 AWS 资源报告为 NON_COMPLIANT 时收到 SNS 通知。

**注意:**您可以替换特定 AWS 服务和 AWS Config 规则的 AWS Config 资源类型规则

1.    如果您尚未创建 Amazon SNS 主题,请按照 Amazon SNS 入门中的说明创建一个。

**重要提示:**Amazon SNS 主题必须与您的 AWS Config 服务位于同一区域。

2.    打开 EventBridge 控制台

3.    选择 Create rule(创建规则)。

4.    在 Name(名称)中,输入您的规则名称。如果需要,输入 Description(描述)。

5.    对于 Rule type(规则类型),选择 Rule with an event pattern(具有事件模式的规则)。然后选择 Next(下一步)。

6.    对于 Event source(事件源),选择 AWS events or EventBridge partner events(AWS 事件或 EventBridge 合作伙伴事件)。

7.    在 Event pattern(事件模式)窗格中,选择 Custom patterns (JSON editor) [自定义模式(JSON 编辑器)],然后复制并粘贴以下示例事件模式:

{
  "source": [
    "aws.config"
  ],
  "detail-type": [
    "Config Rules Compliance Change"
  ],
  "detail": {
    "messageType": [
      "ComplianceChangeNotification"
    ],
    "configRuleName": [
      "ec2-security-group-attached-to-eni"
    ],
    "resourceType": [
      "AWS::EC2::SecurityGroup"
    ],
    "newEvaluationResult": {
      "complianceType": [
        "NON_COMPLIANT"
      ]
    }
  }
}

8.    选择下一步

9.    对于 Target types(目标类型),选择 AWS service(AWS 服务)。

10.    对于 Select a target(选择目标),选择 SNS topic(SNS 主题)。

11.    对于 Topic(主题),选择您的 SNS 主题。

12.    展开 Additional settings(其他设置)。然后,对于 Configure target input(配置目标输入),选择 Input transformer(输入转换器)。

13.选择 Configure input transformer(配置输入转换器)。在 Input Path(输入路径)文本框的 Target input transformer(目标输入转换器)下,复制并粘贴以下示例路径:

{
  "awsRegion": "$.detail.awsRegion",
  "resourceId": "$.detail.resourceId",
  "awsAccountId": "$.detail.awsAccountId",
  "compliance": "$.detail.newEvaluationResult.complianceType",
  "rule": "$.detail.configRuleName",
  "time": "$.detail.newEvaluationResult.resultRecordedTime",
  "resourceType": "$.detail.resourceType"
}

14.    在 Template(模板)文本框中,复制并粘贴以下示例模板。根据使用案例的要求,输入时间规则资源类型、资源 ID、AWS 账户 ID 和 AWS 区域、合规性以及资源信息。

"On <time> AWS Config rule <rule> evaluated the <resourceType> with Id <resourceId> in the account <awsAccountId> region <awsRegion> as <compliance> For more details open the AWS Config console at https://console.aws.amazon.com/config/home?region=<awsRegion>#/timeline/<resourceType>/<resourceId>/configuration"

15.    选择 Confirm(确认)。然后,选择 Next(下一步)。

16.    或者,您可以 Add new tag(添加新标签)。然后,选择 Next(下一步)。

17.    选择 Create rule(创建规则)。

18.    触发某个事件类型后,您将收到一封 SNS 电子邮件通知,其中的自定义字段将从第 13 步填充,与以下所示类似:

"On ExampleTime AWS Config rule ExampleRuleName evaluated the ExampleResourceType with Id ExampleResource_ID in the account ExampleAccount_Id in Region ExampleRegion as ExamplecomplianceType. For more details open the AWS Config console at https://console.aws.amazon.com/config/home?region=ExampleRegion#/timeline/ExampleResourceType/ExampleResource_ID/configuration"

相关信息

对 Route 53 托管区域记录进行更改时如何通知我?

使用 AWS Config 服务在 AWS 账户中创建资源后,如何接收自定义电子邮件通知?

如何配置 EventBridge 规则,以便在特定 AWS 服务事件类型触发时让 GuardDuty 发送自定义的 SNS 通知?

AWS 官方
AWS 官方已更新 2 年前