21

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.

1 Answer 1

30

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.
2
  • 6
    Is it possible to send the logged line together with the alarm message?
    – luislhl
    Commented Jul 9, 2021 at 11:48
  • 6
    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. Commented Jul 9, 2021 at 13:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.