-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from knowit/slack-alarm-forwarder
Slack alarm forwarder
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
import boto3 | ||
import json | ||
import requests | ||
|
||
session = boto3.session.Session() | ||
client = session.client( | ||
service_name='secretsmanager', | ||
region_name="eu-central-1" | ||
) | ||
|
||
def handler(event, context): | ||
attachments = [] | ||
for record in event.get('Records', []): | ||
msg = record.get('Sns', {}).get('Message', "") | ||
if msg: | ||
attachments.append(create_payload(json.loads(msg))) | ||
|
||
requests.post( | ||
url=get_slack_webhook_url(), | ||
json=dict(attachments=attachments) | ||
) | ||
|
||
return dict(status_code=200, body="") | ||
|
||
def create_payload(msg): | ||
colors = dict(OK='good', INSUFFICIENT_DATA='warning', ALARM='danger') | ||
|
||
return { | ||
'mrkdwn_in': ['text'], | ||
'title': f"{msg['Region']} -- {msg['AlarmName']}", | ||
'title_link': f"https://{msg['AWSAccountId']}.signin.aws.amazon.com/console/cloudwatch", | ||
'text': f"Alarm `{msg['AlarmName']}` is in state `{msg['NewStateValue']}`\n\n{msg['NewStateReason']}", | ||
'color': colors.get(msg['NewStateValue'], '#bfbfbf'), | ||
} | ||
|
||
def get_slack_webhook_url(): | ||
get_secret_value_response = client.get_secret_value( | ||
SecretId="slack_webhook_url" | ||
) | ||
response = get_secret_value_response['SecretString'] | ||
slack_webhook_url = json.loads(response)['url'] | ||
|
||
return slack_webhook_url |
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 @@ | ||
requests==2.28.0 |
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