-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[R] Route SNS notifications through a Lambda function (#5246)
- Loading branch information
1 parent
751e60c
commit a2a362b
Showing
11 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import chalice.app | ||
|
||
from azul import ( | ||
cached_property, | ||
) | ||
from azul.chalice import ( | ||
AppController, | ||
) | ||
from azul.indexer.notify_service import ( | ||
EmailService, | ||
) | ||
|
||
|
||
class MonitoringController(AppController): | ||
|
||
@cached_property | ||
def email_service(self): | ||
return EmailService() | ||
|
||
def notify_group(self, event: chalice.app.SNSEvent) -> None: | ||
self.email_service.send_message(event.subject, event.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
import logging | ||
|
||
from azul import ( | ||
config, | ||
) | ||
from azul.deployment import ( | ||
aws, | ||
) | ||
from azul.strings import ( | ||
trunc_ellipses, | ||
) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class EmailService: | ||
|
||
@property | ||
def to_email(self): | ||
return config.monitoring_email | ||
|
||
@property | ||
def from_email(self): | ||
return ' '.join([ | ||
'Azul', | ||
config.deployment_stage, | ||
'Monitoring', | ||
'<monitoring@' + config.api_lambda_domain('indexer') + '>' | ||
]) | ||
|
||
def send_message(self, subject: str, body: str) -> None: | ||
log.info('Sending message %r with body %r', | ||
subject, trunc_ellipses(body, 256)) | ||
try: | ||
body = json.loads(body) | ||
except json.decoder.JSONDecodeError: | ||
log.warning('Not a JSON serializable event, sending as is') | ||
else: | ||
body = json.dumps(body, indent=4) | ||
content = { | ||
'Simple': { | ||
'Subject': { | ||
'Data': subject | ||
}, | ||
'Body': { | ||
'Text': { | ||
'Data': body | ||
} | ||
} | ||
} | ||
} | ||
response = aws.ses.send_email(FromEmailAddress=self.from_email, | ||
Destination=dict(ToAddresses=[self.to_email]), | ||
Content=content) | ||
log.info('Successfully sent message %r, message ID is %r', | ||
subject, response['MessageId']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters