Last active
April 14, 2019 19:54
-
-
Save femioladeji/82e72fa40435579d40a4b17040ce0c95 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
const http = require('https'); | |
const postToSlack = (data) => { | |
const host = 'hooks.slack.com'; | |
const hookUrl = 'slack hook path'; | |
return new Promise((resolve, reject) => { | |
const options = { | |
host, | |
path: hookUrl, | |
method: 'POST', | |
headers: { | |
"Content-Type": "application/json", | |
'Content-Length': Buffer.byteLength(data) | |
} | |
}; | |
const req = http.request(options, (res) => { | |
res.on('end', () => { | |
res.statusCode === 200 | |
? resolve() | |
: reject() | |
}); | |
}); | |
req.write(data); | |
req.end(); | |
}); | |
} | |
exports.handler = async (event, context, callback) => { | |
// get the message passed to the subscription from the SNS topic | |
const message = JSON.parse(event.Records[0].Sns.Message); | |
const host = 'hooks.slack.com'; | |
const hookUrl = '/services/T5ULH901M/BHJ5Y57L2/vZaz2zB7TemNm7t8ofPrc9ns'; | |
const slackMsg = JSON.stringify({ | |
text: `Update on ${message.Trigger.Dimensions[0].value}`, | |
attachments: [{ | |
text: message.NewStateReason, | |
fields: [{ | |
title: 'Lambda Function', | |
value: message.Trigger.Dimensions[0].value, | |
short: true, | |
}, { | |
title: 'Time', | |
value: message.StateChangeTime, | |
short: true, | |
}, { | |
title: 'Alarm', | |
value: message.AlarmName, | |
short: true, | |
}, { | |
title: 'Region', | |
value: message.Region, | |
short: true, | |
}], | |
}] | |
}); | |
try { | |
await postToSlack(slackMsg); | |
callback(); | |
} catch(error) { | |
callback(error) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment