@@ -503,6 +503,27 @@ def format_default(
503
503
return attachments
504
504
505
505
506
+ def parse_notification (message : Dict [str , Any ], subject : Optional [str ], region : str ) -> Optional [Dict ]:
507
+ """
508
+ Parse notification message and format into Slack message payload
509
+
510
+ :params message: SNS message body notification payload
511
+ :params subject: Optional subject line for Slack notification
512
+ :params region: AWS region where the event originated from
513
+ :returns: Slack message payload
514
+ """
515
+ if "AlarmName" in message :
516
+ return format_cloudwatch_alarm (message = message , region = region )
517
+ if message .get ("detail-type" ) == "GuardDuty Finding" :
518
+ return format_guardduty_finding (message = message , region = message ["region" ])
519
+ if message .get ("detail-type" ) == "Security Hub Findings - Imported" :
520
+ return format_aws_security_hub (message = message , region = message ["region" ])
521
+ if message .get ("detail-type" ) == "AWS Health Event" :
522
+ return format_aws_health (message = message , region = message ["region" ])
523
+ if subject == "Notification from AWS Backup" :
524
+ return format_aws_backup (message = str (message ))
525
+ return format_default (message = message , subject = subject )
526
+
506
527
def get_slack_message_payload (
507
528
message : Union [str , Dict ], region : str , subject : Optional [str ] = None
508
529
) -> Dict :
@@ -534,35 +555,10 @@ def get_slack_message_payload(
534
555
535
556
message = cast (Dict [str , Any ], message )
536
557
537
- if "AlarmName" in message :
538
- notification = format_cloudwatch_alarm (message = message , region = region )
539
- attachment = notification
540
-
541
- elif (
542
- isinstance (message , Dict ) and message .get ("detail-type" ) == "GuardDuty Finding"
543
- ):
544
- notification = format_guardduty_finding (
545
- message = message , region = message ["region" ]
546
- )
547
- attachment = notification
548
-
549
- elif isinstance (message , Dict ) and message .get ("detail-type" ) == "Security Hub Findings - Imported" :
550
- notification = format_aws_security_hub (message = message , region = message ["region" ])
551
- attachment = notification
552
-
553
- elif isinstance (message , Dict ) and message .get ("detail-type" ) == "AWS Health Event" :
554
- notification = format_aws_health (message = message , region = message ["region" ])
555
- attachment = notification
556
-
557
- elif subject == "Notification from AWS Backup" :
558
- notification = format_aws_backup (message = str (message ))
559
- attachment = notification
560
-
561
- elif "attachments" in message or "text" in message :
558
+ if "attachments" in message or "text" in message :
562
559
payload = {** payload , ** message }
563
-
564
560
else :
565
- attachment = format_default (message = message , subject = subject )
561
+ attachment = parse_notification (message , subject , region )
566
562
567
563
if attachment :
568
564
payload ["attachments" ] = [attachment ] # type: ignore
@@ -602,7 +598,6 @@ def lambda_handler(event: Dict[str, Any], context: Dict[str, Any]) -> str:
602
598
:param context: lambda expected context object
603
599
:returns: none
604
600
"""
605
- logging .warning (f"Event logging enabled: `{ json .dumps (event )} `" )
606
601
607
602
for record in event ["Records" ]:
608
603
sns = record ["Sns" ]
0 commit comments