Created
May 30, 2021 07:11
-
-
Save zelic91/3405da4878de9ee238e44f866481f826 to your computer and use it in GitHub Desktop.
Lambda Function for Slack Notification
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'); | |
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