diff --git a/alarm_creator/lambda_function.py b/alarm_creator/lambda_function.py index 7e10423..52fbb48 100644 --- a/alarm_creator/lambda_function.py +++ b/alarm_creator/lambda_function.py @@ -1,9 +1,5 @@ -import boto3 -import json -import os -import logging -import traceback -import sys +import boto3, json, os +import logging, traceback, sys import datetime from actions import AWS_Alarms, DeleteAlarms @@ -14,8 +10,9 @@ sns_arn = os.environ["SNS_ARN"] + def lambda_handler(event, context): - print(event) + function_detail = context.invoked_function_arn try: print("{}: AWS_Alarms()".format(datetime.datetime.now())) @@ -28,17 +25,20 @@ def lambda_handler(event, context): traceback_string = traceback.format_exception( exception_type, exception_value, exception_traceback ) + # Create a JSON dump with the details to publish to the SNS topic in case of an exeception. err_msg = json.dumps( { + "functionDetail": function_detail, "errorType": exception_type.__name__, "errorMessage": str(exception_value), "stackTrace": traceback_string, } ) + # Publish the error message to the SNS topic client.publish( TopicArn=f"{sns_arn}", - Subject="Error Creating CloudWatch Alarm", + Subject=f"Error Creating CloudWatch Alarm", Message=json.dumps({"default": err_msg}), MessageStructure="json", )