Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Lambda function to post SES mail updates from SNS and an SES configuration set
var https = require('https');
var util = require('util');
const SLACK_URI = process.env.SLACK_URI || 'XXXX/XXXX/XXXXXXXXXXXXXXXX'
const SLACK_CHANNEL = process.env.SLACK_CHANNEL || 'YOUR-SLACK-CHANNEL'
exports.handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
let messageData = {};
try{
messageData = JSON.parse(event.Records[0].Sns.Message)
console.info(JSON.stringify(messageData, null, 2));
}catch(error){
console.warn(`ERROR parsing SNS message String into JSON - ${event.Records[0].Sns.Message}`);
context.fail();
}
let payload = {
"channel": `#${SLACK_CHANNEL}`,
"username": "AWS SES",
"text": `*${messageData.eventType}* : ${messageData.mail.destination} : _${messageData.mail.commonHeaders.subject}_`,
"icon_emoji": ":email:"
};
try{
await postToSlack(payload);
}catch(error)
{
console.warn(`ERROR posting to Slack: ${error.message}`);
}
};
async function postToSlack(payload)
{
const options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: `/services/${SLACK_URI}`
};
return new Promise(function(resolve, reject){
const req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
resolve()
});
});
req.on('error', function(error) {
reject(error)
});
req.write(util.format("%j", payload));
req.end();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.