Skip to content

Instantly share code, notes, and snippets.

@labocho
Created January 24, 2020 09:50
Show Gist options
  • Save labocho/cb3954343e9b32ea958f7980c084358f to your computer and use it in GitHub Desktop.
Save labocho/cb3954343e9b32ea958f7980c084358f to your computer and use it in GitHub Desktop.
Lambda function (python3.7) to send SNS message to Slack
import urllib.request
import json
import os
# https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sns.html
def lambda_handler(event, context):
req = urllib.request.Request(
os.environ["SLACK_WEBHOOK_URL"],
json.dumps({
"text": event["Records"][0]["Sns"]["Subject"],
"attachments": [{
"text": event["Records"][0]["Sns"]["Message"],
}]
}).encode(),
{"Content-Type": "application/json"},
)
with urllib.request.urlopen(req) as res:
status = res.status
body = res.read().decode("utf-8")
return {
'statusCode': status,
'body': json.dumps(body),
}
# print(lambda_handler({"Records": [{"Sns": {"Subject": "subject", "Message": "message"}}]}, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment