Query STOPPED Tasks within an AWS ECS Service using CloudWatch Log Insights Queries (Fargate Launch Type)

Viewed 29

I've been trying to create an AWS CloudWatch Custom Dashboard to indicate the number of Times a Task running in a ECS Service has STOPPED (Fargate Launch Type) due to containers crashing (could be due to a container related issue or application level issue).

I tried to retrieve the expected metric by writing a CloudWatch Logs Insights Query to query the /aws/ecs/containerinsights/{my-cluster-name}/performance Log Group. The query I used for this is written as below,

fields TaskId, KnownStatus, LaunchType, @timestamp
 | filter ServiceName = "my-first-service" and KnownStatus = "STOPPED"
 | limit 20 

However, this doesn't output the STOPPED instances. Though, it outputs the RUNNING and Pending tasks when filtered by equalling KnownStatus to RUNNING or PENDING.

So my question is, doesn't ECS log STOPPED task logs to CloudWatch?

If it does, appreciate it very much if anyone could provide an example,

  1. to indicate the number of Times a Task had STOPPED?
  2. to indicate the number of times each Container within a Task had STOPPED or crashed?

I also read in the documentation that Amazon EventBridge can be used to detect the STOPPED task events triggered by ECS, which in turn can write its logs back to CloudWatch. Hence, please feel free to advise on what would be the best practice to achieve my requirement.

Thank you.

0 Answers
Related