How to Get Instant Email Notifications for AWS Lambda Errors with CloudWatch Alarms
profile picture Kishan S
5 min read Apr 13, 2023

How to Get Instant Email Notifications for AWS Lambda Errors with CloudWatch Alarms

Do you want to receive an email notification whenever something goes wrong or any message is logged in your AWS Lambda function? With CloudWatch Logs and SNS, it’s easy to set up email alerts based on specific log message filters. This way, you’ll stay updated on your Lambda function’s performance and any issues that might arise.

Here’s how it works:

When your Node.js Lambda function produces logs, they are stored in CloudWatch Logs. You can then create metric filters in CloudWatch Logs to search for specific log messages based on filters such as INFO, ERROR, or WARN. Once you create the filter, you can attach a CloudWatch Alarm to it, which can be subscribed to an SNS topic. This way, you’ll receive an email notification each time the filter is triggered by a log message.

To add filters to your console statements in Node.js, simply include a filter keyword in your log statement. For example:

console.log('[INFO] This is an information message.')
console.log('[ERROR] This is an error message.')
console.log('[WARN] This is a warning message.')

This will make it easier to identify and filter specific log messages in CloudWatch Logs, and receive email notifications when they occur.

pre-requisites?

Before you can set up email notifications for your Lambda function, you’ll need to have:

  1. A Lambda function with any runtime (we’ll be using Node.js in this example).
  2. An SNS topic with an email subscription that has been verified to receive emails.

Assuming you already have a Lambda function with filters added to your console statements, here’s an example code snippet:

exports.handler= async(event) => {
    try {
        const response = {
            statusCode: 400,
            body: JSON.stringify('Error from Lambda!'),
        };
        if(response.statusCode !== 200) throw new Error(response);
    } catch (error) {
        console.log('[ERROR] Error',error)
        return error
    }
};

Once you have a Lambda function and an SNS topic with an email subscription, you can create a CloudWatch Alarm to trigger email notifications based on log message filters.

Step1:Creating a Metric Filter for Your Lambda Logs

  1. Go to the AWS Management Console and navigate to the CloudWatch.
  2. Choose “Logs” on the left navigation panel and select the log group for your Lambda function.
  3. In the log group, navigate to metric Filter section to select “Create Metric Filter” button to create a new filter.
  4. In the “Define Logs Metric Filter” screen, enter a filter pattern. For example, if you want to create a filter for errors, you can use the pattern:
[ERROR]
Screenshot 2023-03-31 at 3.33.43 PM.png Screenshot 2023-03-31 at 3.35.47 PM.png This will match any log entries that include the string "[ERROR]" in them. 5. Next, define the details for your metric filter. Specify filter a name, and the metric details such as metric namespace, metric name, metric value, default value, and unit (optional). Screenshot 2023-03-31 at 3.36.52 PM.png

6 .Click on button “Next” to review the filter settings once you’ve entered the details, click “Create Filter” to create the metric filter.

Step2:Create a CloudWatch Alarm to Send Email Notifications.

The next step is to set up a CloudWatch alarm that will send email notifications based on this filter. Follow the below steps:

  1. In the “Cloud watch log” screen, select the metric filter you created in the previous step as the source of your alarm and click on create alarm.

    Screenshot 2023-03-31 at 3.47.09 PM.png
  2. Next, set up the conditions for your alarm. For example, if you want to be notified when there is at least one error in your Lambda logs during a 5-minute period, you can use the following settings:

    • Metric: “Errors” (or whatever you named your metric filter)
    • Statistic: “Sum”
    • Period: “5 minutes”
    • Conditions: “Greater than or equal to” threshold of “1” Screenshot 2023-03-31 at 3.48.53 PM.png
  3. Input the conditions and threshold value(1 or more), later click on “Next” button to configure the actions for your alarm.

    Screenshot 2023-03-31 at 3.49.17 PM.png
  4. Enter the details for your SNS topic In the “Actions” screen. This should include the name of the SNS topic and the email address you want to send notifications to.

    Screenshot 2023-03-31 at 3.51.53 PM.png
  5. Add Name to your cloudwatch alarm and review the settings and click on “Create Alarm” to create the alarm.

    Screenshot 2023-03-31 at 3.52.19 PM.png

Now that it is configured, your CloudWatch alarm will send email notifications to the chosen email address whenever the criteria you specified are satisfied.

Step3: Now,It’s time to test it out

every thing is done lets trigger the lambda from api gateway,Once the Lambda function is triggered and produces an error, the CloudWatch alarm will be triggered as well.

check the status of the CloudWatch alarm, navigate to the “Alarms” section in the CloudWatch service console. You should see the alarm you created in the list of alarms. If there was an error produced by the Lambda function, the alarm will be in an “In alarm” state.

Finally, check your email inbox to make sure you received the email notification. The email will contain the details of the error that triggered the alarm, along with other relevant information like the time of the error. Screenshot 2023-03-31 at 3.24.52 PM.png

Congratulations! You have successfully set up email notifications for your Lambda function using CloudWatch alarms and SNS topics. This will help you stay on top of any errors that occur in your Lambda function and take corrective action as quickly as possible.

service category

Innovate faster, and go farther with serverless-native application development. Explore limitless possibilities with AntStack's serverless solutions. Empowering your business to achieve your most audacious goals.

Build with us

Author(s)

Your Digital Journey deserves a great story.

Build one with us.

Recommended Blogs

cookie

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.

Build With Us