Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calexandrepcjr/8b8ddc011973b23fbc9941fc9188a84f to your computer and use it in GitHub Desktop.
Save calexandrepcjr/8b8ddc011973b23fbc9941fc9188a84f to your computer and use it in GitHub Desktop.
Sends Slack Message by receiving a cloudwatch log identified by a tag
/**
* EXAMPLE: [ONBOARDING]Someone has just signed up!
**/
const https = require('https');
const zlib = require('zlib');
exports.handler = (event, context, callback) => {
const options = {
hostname: "hooks.slack.com",
method: "POST",
path: "/services/webhook_hash/hash",
};
let message = "";
if (event.awslogs && event.awslogs.data) {
const payload = Buffer.from(event.awslogs.data, 'base64');
const logevents = JSON.parse(zlib.unzipSync(payload).toString()).logEvents;
for (const logevent of logevents) {
message += logevent.message;
}
}
if (message === "") {
return;
}
const bracketPosition = message.indexOf("]");
const newMessage = message.substr(bracketPosition + 1);
const text = newMessage.length < 5 ? message : newMessage;
const payload = JSON.stringify({
'channel': '#slack-channel',
'username': 'Platform Bot',
'text': `*${text}* :partying_face:`,
'icon_emoji': ':aws:',
'attachments': []
});
const req = https.request(options,
(res) => res.on("data", () => callback(null, "OK")))
req.on("error", (error) => callback(JSON.stringify(error)));
req.write(payload);
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment