user1297406
user1297406

Reputation: 1331

Detect and generate a cloudwatch Alarm when a Task in ECS is killed or restarted

I'm trying to set an Alarm using Cloudwatch to detect when a Task is killed in an ECS Cluster.

I followed this https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cloudwatch-metrics.html#cw_running_task_count to set up a metric to track the number of tasks running by a service.

This is my alarm :

**Namespace** : AWS/ECS
**MetricName** : CPUUtilization
**ServiceName** : my_service
**ClusterName** : my_cluster
**Statistic** : Sample count
**Period** : 1minute
**Conditions** :
**Threshold type** : Static
**Whenever CPUUtilization is**... : Lower Than 1

But it's not working and doesn't generate an Alarm as expected. I think that this is because if the task is killed, it's recreated again automatically and quickly under 1 minute (the period set in the Alarm).

I tried to change the Period to less than a minute but AWS says Only a period greater than 60s is supported for metrics in the "AWS/" namespaces

So is there a way to detect if a task is killed ?

Thanks

Upvotes: 5

Views: 8111

Answers (2)

newbie17
newbie17

Reputation: 197

You can set up an Event in cloudwatch to monitor on "ECS Task State Change" and put condition on lastStatus. Here is the code for that-

{
      "source": [
        "aws.ecs"
      ],
      "detail-type": [
        "ECS Task State Change"
      ],
      "detail": {
        "clusterArn": [
          "arn:aws:ecs:us-west-2:1234567891234:cluster/mycluster",
        ],
        "containers": {
          "lastStatus": [
            "STOPPED"
          ]
        }
      }
    }

Upvotes: 2

marianogg9
marianogg9

Reputation: 194

  • According to doc you can trigger an event from ECS.

  • Here eventually you can track a stop event from a task. That way you can use that event as input for a CloudWatch rule.

Upvotes: 0

Related Questions