Search code examples
amazon-ecsamazon-cloudwatch

Creating an CloudWatch Event Rule for failed ECS tasks


I currently have an ECS task that fails from time to time, with different error codes. I would like to create a CloudWatch event rule that is triggered on such failures. I currently have the following cloud watch event rule, which is triggered only on exit code 1. I would like to be notified of all non-zero errors exit code.

{
  "source": [
    "aws.ecs"
  ],
  "detail-type": [
    "ECS Task State Change"
  ],
  "detail": {
    "lastStatus": [
      "STOPPED"
    ],
    "stoppedReason": [
      "Essential container in task exited"
    ],
    "containers": {
      "exitCode": [
        "1"
      ]
    }
  }
}

Solution

  • Now you can use "anything-but" in CW rules.

    {
      "source": [
        "aws.ecs"
      ],
      "detail-type": [
        "ECS Task State Change"
      ],
      "detail": {
        "lastStatus": [
          "STOPPED"
        ],
        "containers": {
          "exitCode": [
            {
              "anything-but": 0
            }
          ]
        }
      }
    }