Created
January 24, 2016 10:03
-
-
Save syamn/afae9227174e1fee668e to your computer and use it in GitHub Desktop.
AWS "CloudWatch to Slack" Lambda function
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
var url = require('url'); | |
var https = require('https'); | |
// TODO: !!! Must be changed following Slack WebHook URL !!! | |
var hookUrl = 'https://hooks.slack.com/services/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'; | |
var processEvent = function(event, context) { | |
var message = JSON.parse(event.Records[0].Sns.Message); | |
// Format Slack posting message | |
var text = "<!channel> *" + message.AlarmDescription + "* state is now `" + message.NewStateValue + "`\n" + | |
"```" + | |
"reason: " + message.NewStateReason + "\n" + | |
"alarm: " + message.AlarmName + "\n" + | |
"time: " + message.StateChangeTime + | |
"```" | |
; | |
var slackMessage = { | |
text: text | |
}; | |
postMessage(slackMessage, function(response) { | |
if (response.statusCode < 400) { | |
console.info('Message posted!'); | |
context.succeed(); | |
} else if (response.statusCode < 500) { | |
console.error("4xx error occured when processing message: " + response.statusCode + " - " + response.statusMessage); | |
context.succeed(); // Don't retry when got 4xx cuz its request error | |
} else { | |
// Retry Lambda func when got 5xx errors | |
context.fail("Server error when processing message: " + response.statusCode + " - " + response.statusMessage); | |
} | |
}); | |
}; | |
var postMessage = function(message, callback) { | |
var body = JSON.stringify(message); | |
var options = url.parse(hookUrl); | |
options.method = 'POST'; | |
options.headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(body), | |
}; | |
var postReq = https.request(options, function(res) { | |
var chunks = []; | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
return chunks.push(chunk); | |
}); | |
res.on('end', function() { | |
var body = chunks.join(''); | |
if (callback) { | |
callback({ | |
body: body, | |
statusCode: res.statusCode, | |
statusMessage: res.statusMessage | |
}); | |
} | |
}); | |
return res; | |
}); | |
postReq.write(body); | |
postReq.end(); | |
}; | |
exports.handler = function(event, context) { | |
if (hookUrl) { | |
processEvent(event, context); | |
} else { | |
context.fail('Missing Slack Hook URL.'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment