Forked from daniel-woods/sns_lambda_slack_webhook.py
Created
September 29, 2018 02:40
-
-
Save vvalorous/41d6af7921134036db50e2fdc59fc80c to your computer and use it in GitHub Desktop.
Create an SNS Topic, with a Lambda function subscribed. Using the below handler, you can parse the vent object for your SNS message and pass it to a Slack webhook URL.
This file contains hidden or 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
#!/usr/bin/python2.7 | |
# https://gist.github.com/daniel-woods | |
import json | |
from urllib2 import urlopen, HTTPError, Request | |
def post_message(url, data): | |
req = Request(url, data) | |
f = urlopen(req) | |
response = f.read() | |
f.close() | |
def lambda_handler(event, context): | |
# Webhook URL, get this from the Slack channel. | |
url = "https://hooks.slack.com/services/XXXXXX/XXXXXXXX/XXXXxxxxXXXXxxxxXXXX" | |
msg = { | |
"channel": "#aws-sns", | |
"username": "webhookbot", | |
"text": event['Records'][0]['Sns']['Message'], | |
"icon_emoji": ":ghost:" | |
} | |
# Send the Message | |
str_msg = json.dumps(msg) | |
data = "payload=" + str_msg | |
post_message(url, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment