AWS EVENTBRIDGE:向 ECS 任务状态更改添加内容过滤

AWS EVENTBRIDGE: Add content filtering to ECS task state changes

每当ECS任务被异常删除时,我正在尝试创建一个eventbridge规则。

通常 ECS 也会发送创建或附加状态的所有事件事件,但我只想过滤 DELETEDstate。

我正在使用 CDK 创建事件规则。我正在尝试根据附件字段中存在的状态实现内容过滤,该字段又是详细信息字段的一部分。

来自 ECS 任务的示例事件 ->

    {
        "version": "0",
        "id": "3317b2af-7005-947d-b652-f55e762e571a",
        "detail-type": "ECS Task State Change",
        "source": "aws.ecs",
        "account": "111122223333",
        "time": "2020-01-23T17:57:58Z",
        "region": "us-west-2",
        "resources": [
            "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad"
        ],
        "detail": {
            "attachments": [
                {
                    "id": "1789bcae-ddfb-4d10-8ebe-8ac87ddba5b8",
                    "type": "eni",
                    "status": "ATTACHED",
                    "details": [
                        {
                            "name": "subnetId",
                            "value": "subnet-abcd1234"
                        },
                        {
                            "name": "networkInterfaceId",
                            "value": "eni-abcd1234"
                        },
                        {
                            "name": "macAddress",
                            "value": "0a:98:eb:a7:29:ba"
                        },
                        {
                            "name": "privateIPv4Address",
                            "value": "10.0.0.139"
                        }
                    ]
                }
            ],
            "availabilityZone": "us-west-2c",
            "clusterArn": "arn:aws:ecs:us-west-2:111122223333:cluster/FargateCluster",
            "containers": [
                {
                    "containerArn": "arn:aws:ecs:us-west-2:111122223333:container/cf159fd6-3e3f-4a9e-84f9-66cbe726af01",
                    "lastStatus": "RUNNING",
                    "name": "FargateApp",
                    "image": "111122223333.dkr.ecr.us-west-2.amazonaws.com/hello-repository:latest",
                    "imageDigest": "sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6",
                    "runtimeId": "ad64cbc71c7fb31c55507ec24c9f77947132b03d48d9961115cf24f3b7307e1e",
                    "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad",
                    "networkInterfaces": [
                        {
                            "attachmentId": "1789bcae-ddfb-4d10-8ebe-8ac87ddba5b8",
                            "privateIpv4Address": "10.0.0.139"
                        }
                    ],
                    "cpu": "0"
                }
            ],
            "createdAt": "2020-01-23T17:57:34.402Z",
            "launchType": "FARGATE",
            "cpu": "256",
            "memory": "512",
            "desiredStatus": "RUNNING",
            "group": "family:sample-fargate",
            "lastStatus": "RUNNING",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "FargateApp"
                    }
                ]
            },
            "connectivity": "CONNECTED",
            "connectivityAt": "2020-01-23T17:57:38.453Z",
            "pullStartedAt": "2020-01-23T17:57:52.103Z",
            "startedAt": "2020-01-23T17:57:58.103Z",
            "pullStoppedAt": "2020-01-23T17:57:55.103Z",
            "updatedAt": "2020-01-23T17:57:58.103Z",
            "taskArn": "arn:aws:ecs:us-west-2:111122223333:task/FargateCluster/c13b4cb40f1f4fe4a2971f76ae5a47ad",
            "taskDefinitionArn": "arn:aws:ecs:us-west-2:111122223333:task-definition/sample-fargate:1",
            "version": 4,
            "platformVersion": "1.3.0"
        }
    }

cdk代码

    {
            eventPattern: {
              source: ['aws.ecs'],
              detailType: ['ECS Task State Change'],
              detail: {
                clusterArn: [cluster.clusterArn],
                attachments: [{ status: [{ prefix: 'DELETED' }] }] // this is not working
              },
            },
          }

EventBridge 可以匹配数组中的标量,但不能匹配数组中的任意对象:

docs: If the value in the event is an array, then the event pattern matches if the intersection of the event pattern array and the event array is non-empty.

这意味着 EventBridge 不能只匹配 "status": "DELETED"。你有什么选择?

  1. 将您的模式建立在相关的 non-array key-value 对上,例如"lastStatus": "STOPPED".
  2. 匹配所有模式。向事件目标添加逻辑以忽略不感兴趣的模式。

注意:因为你说数组可靠地只有一个元素,所以你可以 transform the event detail 在它被发送到目标之前。这对匹配问题没有帮助,但可以使下游过滤更容易。这是 Lambda 目标的 CDK 示例:

rule.addTarget(
  new targets.LambdaFunction(func, {
    event: events.RuleTargetInput.fromObject({
      status: events.EventField.fromPath('$.detail.attachments[0].status'),
      original: events.EventField.fromPath('$'),
    }),
  })
);

Lambda 接收重塑的事件详细信息:

{
  "status": "ATTACHED",
  "original": <the original event>
}