-
Notifications
You must be signed in to change notification settings - Fork 18
/
cloudformation-notifications.py
38 lines (32 loc) · 1.24 KB
/
cloudformation-notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import boto3
def handler(event, context):
# Notification types
env_notification_types = os.getenv("NOTIFICATION_TYPES", None)
notification_types = env_notification_types.split(",") if env_notification_types else None
if not notification_types:
print("At least one CloudFormation notification type needs to be specified")
return
# SNS topic ARN
sns_topic_arn = os.getenv("SNS_TOPIC_ARN", None)
if not sns_topic_arn:
print("The ARN of the SNS topic needs to be specified")
return
try:
message = str(event["Records"][0]["Sns"]["Message"]).replace("\n", ",")
except Exception:
print("Message could not be parsed. Event: %s" % (event))
return
# Ignore resources that are not the CloudFormation stack itself
if "ResourceType='AWS::CloudFormation::Stack'" not in message:
return
for notification_type in notification_types:
if notification_type not in message:
continue
sns_subject = "CloudFormation %s" % (notification_type)
sns_message = message.replace(",", "\n")
boto3.client('sns').publish(
Subject=sns_subject,
Message=sns_message,
TopicArn=sns_topic_arn
)