Skip to content

Instantly share code, notes, and snippets.

@toshke
Created May 29, 2018 06:19
Show Gist options
  • Save toshke/18716d6cb912322589faf90703e9fb9c to your computer and use it in GitHub Desktop.
Save toshke/18716d6cb912322589faf90703e9fb9c to your computer and use it in GitHub Desktop.
AWS Lambda SNS to Slack Message
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