-
-
Save vvalorous/99a7a13ef73c6229d873a364e30c8648 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'); | |
var webhook = '/services/webhook'; | |
var errorMessage = "ERROR"; | |
exports.handler = function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
console.log('From SNS:', event.Records[0].Sns.Message); | |
var postData = { | |
"channel": "#monitoring", | |
"icon_emoji": ":cloud:", | |
}; | |
var message = JSON.parse(event.Records[0].Sns.Message); | |
postData.attachments = [{ | |
"text": message.Description, | |
"title": "AWS ASG Notifications", | |
"color": getStatusColor(message.Event), | |
}]; | |
try { | |
postData.attachments[0]["fields"] = []; | |
for (var key of ['AutoScalingGroupName', 'Cause', 'Event', 'EC2InstanceId', 'Progress', 'StartTime']) { | |
var field = { | |
title: key, | |
value: message[key], | |
short: false | |
}; | |
postData.attachments[0]["fields"].push(field); | |
} | |
} catch (err) { | |
console.log("Message body was not a JSON payload"); | |
} | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: webhook | |
}; | |
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(); | |
}; | |
String.prototype.contains = function(str, ignoreCase) { | |
return (ignoreCase ? this.toUpperCase() : this) | |
.indexOf(ignoreCase ? str.toUpperCase() : str) >= 0; | |
}; | |
/* returns a Slack color depending on the modulus event type. Crash should be red, deploy should be | |
* green, and everything else will be orange. | |
*/ | |
var getStatusColor = function(messageEvent) { | |
if(messageEvent.contains(errorMessage)) { | |
return 'danger'; | |
} else { | |
return 'warning'; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment