How to Set Up Automated Email Alerts for AWS Lambda Errors
2025-05-04
AWS CloudWatch is great at many things, but it's certainly not the best when it comes to notifying you about issues. If you've ever worked with metrics or logs, you know the tedious drill: set up what to monitor using AWS's own strange pattern matching, create an SNS/SQS to receive the notification, configure SES email settings, create the correct permissions, and finally spend a couple more hours debugging it—all just to get a simple alert when something breaks.
Our Solution
At Opali, we do things differently: a simple cron-triggered Lambda function that looks through the logs and sends an email when an error is detected. This Lambda + email setup has greatly simplified our internal processes: being fully customizable, you can use your preferred scripting language and get it working in just a few minutes.
However, emails are a bit of a pain to set up, which is why we created our own solution: hook2email. It allows us to set up a notification API hook that transforms a REST call into an email!
Set Up
Here's how you can do it:
1. Create a hook
- Sign up at hook2email.com
- Create a template email, like:
Subject:Error found in {function_name}
Body:An error was detected in Lambda function: {function_name} Log message: {error_message} Timestamp: {timestamp}
- Add the recipients who should receive the alert.
2. Get error logs from CloudWatch
Here's a Python snippet (using boto3) that scans your Lambda's CloudWatch logs for recent errors:
import boto3
import datetime
import requests
# Set up clients
logs = boto3.client('logs')
log_group = '/aws/lambda/YOUR_FUNCTION_NAME'
now = datetime.datetime.utcnow()
start_time = int((now - datetime.timedelta(minutes=10)).timestamp() * 1000)
# Search for [ERROR] in the last 10 minutes
response = logs.filter_log_events(
logGroupName=log_group,
startTime=start_time,
filterPattern='[ERROR]'
)
for event in response.get('events', []):
error_message = event['message']
timestamp = datetime.datetime.utcfromtimestamp(event['timestamp']/1000).isoformat()
# Send to hook2email
requests.post(
'https://hook2email.com/hooks/YOUR_HOOK_ID/send',
json={
"function_name": "YOUR_FUNCTION_NAME",
"error_message": error_message,
"timestamp": timestamp
}
)
Replace YOUR_FUNCTION_NAME
and YOUR_HOOK_ID
with your actual Lambda function name and hook2email hook ID.
3. Set up the cron job
To run this check automatically, set up a scheduled Lambda (using EventBridge):
- Go to the AWS Lambda console and create a new function (Python 3.9+).
- Paste the code above into your Lambda handler.
- Add the necessary IAM permissions for
logs:FilterLogEvents
andlogs:DescribeLogGroups
. - In the Lambda's "Triggers" tab, add a new EventBridge (CloudWatch Events) rule:
- Schedule expression:
rate(10 minutes)
- Schedule expression:
- Deploy!
And voila! You'll now get an email whenever an error is detected in your Lambda logs—no more missed issues, and no more wrestling with AWS's notification setup.
Let us know if you have any questions or want to see more automation tips!