Skip to content

Instantly share code, notes, and snippets.

@johnnaegle
Last active December 15, 2020 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save johnnaegle/b92c2812fe0b3f05e00f3d6250c0b98f to your computer and use it in GitHub Desktop.
Save johnnaegle/b92c2812fe0b3f05e00f3d6250c0b98f to your computer and use it in GitHub Desktop.
Posts an Amazon Autoscaling SNS message to a slack webhook
var https = require('https');
var util = require('util');
var webhook = '/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX';
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
var postData = {
"text": "*" + event.Records[0].Sns.Subject + "*"
};
var message = event.Records[0].Sns.Message;
postData.attachments = [{
"text": message
}];
try {
message = JSON.parse(message);
postData.attachments[0].text = message.Description;
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();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment