1717from __future__ import print_function
1818from graffiti_monkey import cli as gm_cli
1919import os
20+ import boto3
2021import logging
2122
2223# Remove existing log handler setup by Lambda
@@ -32,11 +33,25 @@ def envvar_to_list(envvar):
3233 return os .environ [envvar ].split (',' )
3334
3435
36+ def send_notification (sns_arn , region , error ):
37+ client = boto3 .client ('sns' )
38+
39+ response = client .publish (
40+ TopicArn = sns_arn ,
41+ Message = 'Error running Lambda Graffiti Monkey in ' + region + '. Error Message: ' + error
42+
43+ )
44+
45+ log .info ('SNS Response: {}' .format (response ))
46+
47+
3548def handler (event , context ):
3649 log .info ('Loading function' )
3750 try :
51+ sns_arn = os .environ ['SNS_ARN' ]
52+ region = os .environ ['REGION' ]
3853 gm = gm_cli .GraffitiMonkeyCli ()
39- gm .region = os . environ [ 'REGION' ]
54+ gm .region = region
4055 gm .config = {"_instance_tags_to_propagate" : envvar_to_list ('INSTANCE_TAGS_TO_PROPAGATE' ),
4156 "_volume_tags_to_propagate" : envvar_to_list ('VOLUME_TAGS_TO_PROPAGATE' ),
4257 "_volume_tags_to_be_set" : envvar_to_list ('VOLUME_TAGS_TO_BE_SET' ),
@@ -47,6 +62,12 @@ def handler(event, context):
4762 gm .start_tags_propagation ()
4863 return 'Graffiti Monkey completed successfully!'
4964 except KeyError , e :
50- log .error ('Error: Environment variable not set: ' + str (e ))
65+ error_message = 'Error: Environment variable not set: ' + str (e )
66+ log .error (error_message )
67+ log .info ('Sending SNS message to ' + sns_arn )
68+ send_notification (sns_arn , region , error_message )
5169 except Exception , e :
52- log .error ('Error: Graffiti Monkey encountered the following error: ' + str (e ))
70+ error_message = 'Error: Graffiti Monkey encountered the following error: ' + str (e )
71+ log .error (error_message )
72+ log .info ('Sending SNS message to ' + sns_arn )
73+ send_notification (sns_arn , region , error_message )
0 commit comments