Last active
December 15, 2020 21:46
-
-
Save johnnaegle/b92c2812fe0b3f05e00f3d6250c0b98f to your computer and use it in GitHub Desktop.
Posts an Amazon Autoscaling SNS message to a slack webhook
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/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