Created
May 29, 2018 06:19
-
-
Save toshke/18716d6cb912322589faf90703e9fb9c to your computer and use it in GitHub Desktop.
AWS Lambda SNS to Slack Message
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
import json | |
import base64 | |
import sys | |
import os | |
def lambda_handler(event, context): | |
url = os.environ['slack_incoming_hook'] | |
headers = { | |
'Cache-Control': "no-cache" | |
} | |
message = event['Records'][0]['Sns']['Message'] | |
topic = event['Records'][0]['Sns']['TopicArn'] | |
print(f"Mesage from topic {topic}:\n{message}") | |
slack_payload = { | |
'channel': os.environ['slak_channel'], | |
'text': message | |
} | |
if 'slack_username' in os.environ: | |
slack_payload['username'] = os.environ.get('slack_username') | |
if 'slack_icon' in os.environ: | |
slack_payload['icon_emoji'] = os.environ.get('icon_emoji') | |
response = requests.request("POST", url, data=payload, headers=headers) | |
print(response.text) | |
return 'OK' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment