You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS CloudWatch规则配置异常:ECS Fargate任务退出触发逻辑错误

问题原因与解决方案

你的CloudWatch事件规则存在匹配逻辑错误:当前写法将containers.name和containers.exitCode作为两个独立条件,即成功规则会匹配「存在名为test的容器 且 任务中任意容器退出码为0」,错误规则匹配「存在名为test的容器 且 任务中任意容器退出码非0」。当test容器非0退出时,若任务中其他容器(如db/engine)退出码为0,两个规则的条件都会被满足,导致同时触发。

正确的规则配置

需要将name和exitCode条件绑定到同一个容器对象中,确保规则仅针对test容器自身的退出码进行判断:

成功退出触发规则

{
  "source": ["aws.ecs"],
  "detail-type": ["ECS Task State Change"],
  "detail": {
    "taskDefinitionArn": ["<task_arn>"],
    "lastStatus": ["STOPPED"],
    "containers": [
      {
        "name": ["local.images.test"],
        "exitCode": [0]
      }
    ]
  }
}

错误退出触发规则

{
  "source": ["aws.ecs"],
  "detail-type": ["ECS Task State Change"],
  "detail": {
    "taskDefinitionArn": ["<task_arn>"],
    "lastStatus": ["STOPPED"],
    "containers": [
      {
        "name": ["local.images.test"],
        "exitCode": [{"anything-but": 0}]
      }
    ]
  }
}

修改后,成功规则仅会在test容器以0退出码停止时触发,错误规则仅会在test容器以非0退出码停止时触发,不会再出现同时触发的问题。

内容的提问来源于stack exchange,提问作者Moravas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 00:35:22