Created
July 16, 2019 13:23
-
-
Save yorickvP/a6cdcbef27d4dadcb6e68fa30a329258 to your computer and use it in GitHub Desktop.
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'); | |
function postToSlack(postData) { | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: process.env.SLACK_WEBHOOK | |
}; | |
return new Promise((resolve, reject) => { | |
const req = https.request(options, (res) => { | |
let data = "" | |
res.on('data', chunk => { | |
data += chunk.toString('utf8'); | |
}); | |
res.on('end', () => resolve(data)); | |
}); | |
req.on('error', reject); | |
req.write(util.format("%j", postData)); | |
req.end(); | |
}) | |
} | |
exports.handler = async function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
const message = JSON.parse(event.Records[0].Sns.Message); | |
console.log('From SNS:', message); | |
var postData = { | |
"channel": process.env.SLACK_CHANNEL, | |
"username": "AWS SNS", | |
"text": "*" + (event.Records[0].Sns.Subject || message.detail.eventName) + "*", | |
"icon_emoji": ":aws:" | |
}; | |
const attachments = []; | |
if (message.detail.eventName == "ChangeResourceRecordSets") { | |
for(const change of message.detail.requestParameters.changeBatch.changes) { | |
const rrs = change.resourceRecordSet; | |
attachments.push({ | |
"fallback": `${change.action} ${rrs.name} ${rrs.type} (was: ${rrs.resourceRecords[0].value})`, | |
"author_name": message.detail.userIdentity.userName, | |
"footer_icon": 'https://pub.yori.cc/logos-aws-route53.png', | |
"footer": "AWS Route53", | |
"title": `${change.action} ${rrs.name} ${rrs.type}`, | |
"text": "value: " + rrs.resourceRecords.map(x=>x.value).join(", "), | |
"color": {"CREATE": "#2eb886", "DELETE": "#b8572e"}[change.action] || "#392eb8", | |
"ts": (+new Date(message.detail.eventTime))/1000 | |
}); | |
} | |
} | |
postData.attachments = attachments; | |
await postToSlack(postData); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment