From 45d477a35fd108ed70fa4efe0b0b72e5419b0085 Mon Sep 17 00:00:00 2001 From: "andrew@technative.eu" Date: Wed, 11 Sep 2024 11:20:56 +0200 Subject: [PATCH] Updated err_msg with the lambda ARN incase the lambda function might have an error to also display the Lambda ARN in the error details --- alarm_creator/lambda_function.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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", )