Created
April 11, 2017 19:16
-
-
Save mhayes/28ec0d2e05920e2f8752683bee81d1d1 to your computer and use it in GitHub Desktop.
SNS to Slack via Lambda
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
const https = require("https"); | |
const util = require("util"); | |
const SLACK_WEBHOOK_PATH = process.env.SLACK_WEBHOOK_PATH; | |
const SLACK_CHANNEL = process.env.SLACK_CHANNEL; | |
const SLACK_USERNAME = process.env.SLACK_USERNAME || "aws"; | |
const SLACK_EMOJI = process.env.SLACK_EMOJI || ":aws:"; | |
function noop (ret) { | |
if (ret) { | |
return ret; | |
} | |
} | |
function notifySlack (payload) { | |
var options = { | |
method: "POST", | |
hostname: "hooks.slack.com", | |
port: 443, | |
path: SLACK_WEBHOOK_PATH | |
}; | |
var req = https.request(options, function (res) { | |
res.setEncoding("utf8"); | |
res.on("data", noop); | |
}); | |
req.on("error", function (e) { | |
console.log("[ERROR] Problem with request: " + e.message); | |
}); | |
req.write(util.format("%j", payload)); | |
req.end(function () { | |
console.log("Done."); | |
}); | |
} | |
exports.handler = function (event, context, callback) { | |
(event.Records || []).forEach(function (rec) { | |
if (rec.Sns) { | |
notifySlack({ | |
"channel": SLACK_CHANNEL, | |
"username": SLACK_USERNAME, | |
"text": "*" + rec.Sns.Subject + "* - " + rec.Sns.Message, | |
"icon_emoji": SLACK_EMOJI, | |
"mrkdwn": true | |
}); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment