15

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.

Amanda Ferrari
  • 1,168
  • 5
  • 17
  • 30

1 Answers1

23

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.
Jason Wadsworth
  • 8,059
  • 19
  • 32
  • 3
    Is it possible to send the logged line together with the alarm message? – luislhl Jul 09 '21 at 11:48
  • 3
    To do that you'd need to trigger a Lambda function (or some other code) and have it get the logs based on the time. There isn't really anything else that I can think of. I know that is a solution a lot of teams use. – Jason Wadsworth Jul 09 '21 at 13:59