Created
May 14, 2020 08:16
-
-
Save MPJHorner/a07d5911828bdc97036ef05dac8f4536 to your computer and use it in GitHub Desktop.
AWS NodeJS Lambda SES SNS Cloudwatch - Tracking Open, Sends & Deliveries
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
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); | |
}); | |
} | |
}); | |
} | |
} | |
}; |
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 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); | |
} |
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
{ | |
"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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.