Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send alert based on log message on CloudWatch

I have a lambda that sends to STDERR when a given operation fails, something like:

async function handler(event, context) {
  const success = do()
  if (success) {
    return { statusCode: 200 }
  }
  console.error('Failed :(')
  return { statusCode: 400 }
}

This is very simplified, but you get the idea. Naturally, this message will appear on CloudWatch. I would like to know if it's possible (and how to) setup a CloudWatch Alarm to send me an email if this message shows up in my logs.

I've read the docs about CloudWatch alarms, but It's very cluttered and hard to find anything there.

like image 339
Amanda Ferrari Avatar asked Sep 12 '25 09:09

Amanda Ferrari


1 Answers

It's basically a three (kind of four) step process.

  1. You need to create a Metric Filter from your logs. This will allow you to create a metric whenever there is an "error" in your log (or whatever other condition you want). The name of the metric would typically be something like "Errors" for this case, but there is a namespace that is fully yours. In that spot you put something like "/my-organization/my-service" or whatever makes sense to you.
  2. Create a Metric Alarm. This alarm is where you will specify what conditions trigger the alarm. For example, if there is 1 error in any 2 minutes. This alarm will be pointed at the new metric you created in the previous step.
  3. Send the alarm to an SNS topic.
  4. Subscribe to the SNS topic with your email.
like image 85
Jason Wadsworth Avatar answered Sep 15 '25 01:09

Jason Wadsworth