19

Is it possible to set alarms based on CloudWatch Logs Insights queries? In this page it says the following: In addition, you can publish log-based metrics, create alarms, and correlate logs and metrics together in CloudWatch Dashboards for complete operational visibility. but i can't figure out how to do that. I would like to have a custom metric based on some query, add it to a dashboard (which i know how to do) and set an alarm on that.

jimmone
  • 446
  • 1
  • 6
  • 15

2 Answers2

17

I think this is ambiguous wording on their part and the focus of that sentence is really to highlight some of the other capabilities of CloudWatch that can be brought together in the dashboard. Alarms are created based on one or more Metrics (pushed via the CLI or an API).

The announcement on the AWS Blog spins it a slightly different way: CloudWatch Integration – You can write a bit of glue code to run queries, use the results to publish Custom Metrics. Then you can visualize them, set alarms, and so forth, all with the goal of simplifying and accelerating your troubleshooting.

Patrick
  • 703
  • 6
  • 16
  • In other words, it sounds like to get log insights results into an alarm you'd need to write a Lambda function to run the queries and publish metrics from them. – Tim Malone Aug 27 '20 at 23:02
  • 3
    Also - here's someone who's done just that: https://blog.smirnov.la/cloudwatch-logs-insights-to-metrics-a2d197aac379 – Tim Malone Aug 27 '20 at 23:07
  • 2
    How cumbersome to not be able to run a query, and create a metric based on its count with a click of a button (selecting a timespan too of course). – Mike de Klerk Jun 21 '22 at 06:04
6

You could go into "Log Groups" in the Log section of cloudwatch.
There you can select your log group and in "Actions" -> create metric filter.
Once you setup that metric you can attach it to a Alarm.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CreateMetricFilterProcedure.html

codecodex
  • 79
  • 1
  • 1
  • Addendum: Before the alarm can be created from a log group's metric filter, you must choose which metric the filter will push to. If the metric already exists, then you're good to proceed. However, if you need a custom metric, you can only create one via the AWS CLI or the API. – Mark Maglana Apr 07 '22 at 22:10
  • Correction to my earlier comment: You can, in fact, define a custom metric name from the console. When you create the metric filter, just type a custom name in the "Metric name" field (no spaces) and it will be automatically created as soon as the filter has something to add to it. – Mark Maglana Apr 07 '22 at 22:13