Skip to content

Instantly share code, notes, and snippets.

@MPJHorner
Created May 14, 2020 08:16
Show Gist options
  • Save MPJHorner/a07d5911828bdc97036ef05dac8f4536 to your computer and use it in GitHub Desktop.
Save MPJHorner/a07d5911828bdc97036ef05dac8f4536 to your computer and use it in GitHub Desktop.
AWS NodeJS Lambda SES SNS Cloudwatch - Tracking Open, Sends & Deliveries
module.exports = function (config) {
var cloudwatchlogs = config.cloudwatchlogs,
logGroupName = config.logGroupName || '/aws-cloudwatch-logs/default';
return {
log: function (eventName, message, done) {
var logCallback = done || function () {},
d = new Date(),
// logStreamName = config.logStreamName || ([d.getFullYear(), d.getMonth()+1, d.getDate()].join('/') + ' ' + eventName);
logStreamName = eventName;
cloudwatchlogs.describeLogStreams({
logGroupName: logGroupName,
logStreamNamePrefix: logStreamName
}, function (err, data) {
if (err || !data) return logCallback(err);
if (data.logStreams && data.logStreams[0]) {
cloudwatchlogs.putLogEvents({
logEvents: [{
message: JSON.stringify(message),
timestamp: (new Date).getTime()
}],
logGroupName: logGroupName,
logStreamName: logStreamName,
sequenceToken: data.logStreams[0].uploadSequenceToken
}, logCallback);
} else {
cloudwatchlogs.createLogStream({
logGroupName: logGroupName,
logStreamName: logStreamName
}, function (err, data) {
if (err) return logCallback(err);
cloudwatchlogs.putLogEvents({
logEvents: [{
message: JSON.stringify(message),
timestamp: (new Date).getTime()
}],
logGroupName: logGroupName,
logStreamName: logStreamName
}, logCallback);
});
}
});
}
}
};
var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
var yourTopicArn = 'arn:aws:sns:eu-west-2:12345678:Your-Topic'
var cloudwatchlogs = new AWS.CloudWatchLogs({
apiVersion: '2014-03-28'
});
var logGroupName = "ses-email"
var defaultLogStreamName = "MISC"
var createLogger = require('aws-cloudwatch-logs'),
apiLogger = createLogger({
cloudwatchlogs: cloudwatchlogs,
logGroupName: logGroupName
});
if(event.hasOwnProperty('Records')){
for(let i in event.Records){
let record = event.Records[i];
if(record.hasOwnProperty('Sns')){
if(record.Sns.TopicArn == yourTopicArn){
var data = JSON.parse(record.Sns.Message);
logStreamName = extractEmails(data.mail.source)[0];
apiLogger.log(logStreamName, data);
}
}
}
}
callback(null, 'SES Lambda Store Complete');
};
function extractEmails (text)
{
return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
{
"name": "amazon-ses-log-to-cloudwatch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.675.0",
"lambda-log-json": "^1.4.0"
}
}
@MPJHorner
Copy link
Author

This logs SES email events to cloudwatch in JSON format so they are easily searchable.
I created this since moving away from Sendgrid and missing the ability to trace back email sends, clicks, deliveries and open tracking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment