Skip to content

Instantly share code, notes, and snippets.

@zelic91
Created May 30, 2021 07:11
Show Gist options
  • Save zelic91/3405da4878de9ee238e44f866481f826 to your computer and use it in GitHub Desktop.
Save zelic91/3405da4878de9ee238e44f866481f826 to your computer and use it in GitHub Desktop.
Lambda Function for Slack Notification
var https = require('https');
var util = require('util');
exports.handler = (event, context) => {
const message = JSON.parse(event.Records[0].Sns.Message);
console.log(JSON.stringify(event, null, 2));
console.log('Data From SNS: ', message);
const postData = {
'channel': <Slack channel>,
'username': 'AWS CloudWatch Alert',
'text': '*' + message.AlarmName + ' [ ' + message.StateChangeTime + ' ]*',
'attachments': [
{
'text': message.NewStateReason,
'color': 'danger',
'title': 'Reason'
}
]
};
const options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: <Slack webhook>
}
var request = https.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
context.done(null);
});
});
request.on('error', e => {
console.log('Error with the request: ' + e.message);
});
request.write(util.format('%j', postData));
request.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment