I have a small springboot application, its logs are collected by AWS CloudWatch.
I’d like to catch any ERROR in the logs and let the lambda send an email with the error.
So far I have created a filter with the word ERROR in CloudWatch, attach a lambda and it works here the code:
import boto3
import os
import json
import base64
import gzip
logs_client = boto3.client('logs')
sns_client = boto3.client('sns')
def lambda_handler(event, context):
try:
compressed_payload = base64.b64decode(cw_data)
uncompressed_payload = gzip.decompress(compressed_payload)
payload = json.loads(uncompressed_payload)
full_log=""
log_events = payload['logEvents']
for log_event in log_events:
full_log = full_log.join(str(log_event))
print(f'data: {full_log}')
sns_client.publish(
TopicArn=os.environ['SNS_TOPIC_ARN'],
Subject='[500] module: ACME',
Message=full_log
)
except KeyError as e:
error_messages=f"KeyError: {str(e)}"
sns_client.publish(
TopicArn=os.environ['SNS_TOPIC_ARN'],
Subject='Lamda error: reading cloudWatch ...',
Message=error_messages
)
return {
'statusCode': 200,
'body': 'Notification sent!'
}
the problem here, as you can image, is that I get only the single line where the word ERROR appears, while I’d like to have the whole stack trace. I was wondering if there is a way to get the entirely logstream from which the error was spotted