Skip to content

Commit 509d741

Browse files
Merge pull request #8 from joseph-holland/Issue#6
Issue#6 - Add notification functionality via SNS
2 parents b7d771d + 6ab9c06 commit 509d741

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ First we need to set some environment variables for Graffiti Monkey to use, so j
2626

2727
```sh
2828
(graffiti-monkey) $ export REGION="eu-west-1"
29+
(graffiti-monkey) $ export SNS_ARN="arn:aws:sns:eu-west-1:123456654321:lambda-graffiti-monkey"
2930
(graffiti-monkey) $ export INSTANCE_TAGS_TO_PROPAGATE="Name,device,instance_id"
3031
(graffiti-monkey) $ export VOLUME_TAGS_TO_PROPAGATE="Name,device,instance_id"
3132
(graffiti-monkey) $ export VOLUME_TAGS_TO_BE_SET=""

service.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import print_function
1818
from graffiti_monkey import cli as gm_cli
1919
import os
20+
import boto3
2021
import 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+
3548
def 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

Comments
 (0)