-
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.
[u] Route SNS notifications through a Lambda function (#5246)
- Loading branch information
1 parent
0ed80a0
commit 9e96c49
Showing
8 changed files
with
151 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
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 ( | ||
AzulEmailNotificationService, | ||
) | ||
|
||
|
||
class NotifyController(AppController): | ||
|
||
@cached_property | ||
def service(self): | ||
return AzulEmailNotificationService() | ||
|
||
def notify_group(self, event: chalice.app.SNSEvent) -> None: | ||
self.service.notify_group(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,46 @@ | ||
import json | ||
import logging | ||
|
||
from azul import ( | ||
JSON, | ||
config, | ||
) | ||
from azul.deployment import ( | ||
aws, | ||
) | ||
from azul.strings import ( | ||
trunc_ellipses, | ||
) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class AzulEmailNotificationService: | ||
|
||
def notify_group(self, subject: str, message: str) -> None: | ||
log.info('Notifying group of event %r', trunc_ellipses(message, 256)) | ||
# `message` is deserialized to a Python object to be re-serialized as a | ||
# printable JSON | ||
body = json.loads(message) | ||
response = aws.ses.send_email( | ||
FromEmailAddress=f'notify@{config.domain_name}', | ||
Destination={ | ||
'ToAddresses': [config.monitoring_email] | ||
}, | ||
Content=self._format_content(subject, json.dumps(body, indent=4)) | ||
) | ||
log.info('Notification sent, %r', response['MessageId']) | ||
|
||
def _format_content(self, subject: str, body: str) -> JSON: | ||
return { | ||
'Simple': { | ||
'Subject': { | ||
'Data': subject | ||
}, | ||
'Body': { | ||
'Text': { | ||
'Data': body | ||
} | ||
} | ||
} | ||
} |
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