-
-
Save jessefulton/657d0b1c009a94380743 to your computer and use it in GitHub Desktop.
AWS Lambda function to Slack Channel hookup
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 https = require('https'); | |
var util = require('util'); | |
//via http://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects | |
function flatten(data) { | |
var result = {}; | |
function recurse (cur, prop) { | |
if (Object(cur) !== cur) { | |
result[prop] = cur; | |
} else if (Array.isArray(cur)) { | |
for(var i=0, l=cur.length; i<l; i++) | |
recurse(cur[i], prop + "[" + i + "]"); | |
if (l == 0) | |
result[prop] = []; | |
} else { | |
var isEmpty = true; | |
for (var p in cur) { | |
isEmpty = false; | |
recurse(cur[p], prop ? prop+"."+p : p); | |
} | |
if (isEmpty && prop) | |
result[prop] = {}; | |
} | |
} | |
recurse(data, ""); | |
return result; | |
} | |
function parseMessage(msg) { | |
try { | |
var obj = JSON.parse(msg); | |
var flattened = flatten(obj); | |
var info = []; | |
for (var key in flattened) { | |
info.push("*" + key + "*: " + flattened[key]); | |
} | |
return info.join("\n"); | |
} | |
catch (e) { | |
return msg; | |
} | |
} | |
exports.handler = function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
console.log('From SNS:', event.Records[0].Sns.Message); | |
var postData = { | |
// "channel": "#devops", | |
// "username": "Amazon SNS", | |
// "icon_emoji": ":aws:", | |
"text": "*" + event.Records[0].Sns.Subject + "*" | |
}; | |
var message = parseMessage(event.Records[0].Sns.Message); | |
var butWithErrors = message.indexOf(" but with errors"); | |
var stateRed = message.indexOf(" to RED"); | |
var stateYellow = message.indexOf(" to YELLOW"); | |
var noPermission = message.indexOf("You do not have permission"); | |
var failedDeploy = message.indexOf("Failed to deploy application"); | |
var removedInstance = message.indexOf("Removed instance "); | |
var addingInstance = message.indexOf("Adding instance "); | |
var failedConfig = message.indexOf("Failed to deploy configuration"); | |
var failedQuota = message.indexOf("Your quota allows for 0 more running instance"); | |
var abortedOperation = message.indexOf(" aborted operation."); | |
var color = "good"; | |
if (stateRed != -1 || butWithErrors != -1 || noPermission != -1 || failedDeploy != -1 || failedConfig != -1 || failedQuota != -1) { | |
color = "danger"; | |
} | |
if (stateYellow != -1 || removedInstance != -1 || addingInstance != -1 || abortedOperation != -1) { | |
color = "warning"; | |
} | |
postData.attachments = [ | |
{ | |
"color": color, | |
"text": message, | |
"mrkdwn_in": ["text"] | |
} | |
]; | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: '/services/your-slack-webhook-url-info-goes-here' | |
}; | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
context.done(null); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
req.write(util.format("%j", postData)); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment