Last active
August 16, 2022 18:59
-
-
Save calexandrepcjr/8b8ddc011973b23fbc9941fc9188a84f to your computer and use it in GitHub Desktop.
Sends Slack Message by receiving a cloudwatch log identified by a tag
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
/** | |
* 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