Skip to content

Instantly share code, notes, and snippets.

@femioladeji
Last active April 14, 2019 19:54
Show Gist options
  • Save femioladeji/82e72fa40435579d40a4b17040ce0c95 to your computer and use it in GitHub Desktop.
Save femioladeji/82e72fa40435579d40a4b17040ce0c95 to your computer and use it in GitHub Desktop.
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