Last active
January 15, 2019 19:04
-
-
Save aaronhoffman/c2b7ef532b7a28e99aae to your computer and use it in GitHub Desktop.
Lambda function to process a Amazon SES Delivery Notification message from a SNS Topic into a DynamoDB Table
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
{ | |
"Records": [ | |
{ | |
"EventSource":"aws:sns", | |
"EventVersion":"1.0", | |
"EventSubscriptionArn":"arn:aws:sns:us-west-2:xxxx:xxxx", | |
"Sns": { | |
"Type":"Notification", | |
"MessageId":"88B1B251-2E92-4FC3-BFAA-E3BBD0BAB10A", | |
"TopicArn":"arn:aws:sns:us-west-2:881222951025:survey-tool-ses-delivery", | |
"Subject":null, | |
"Message":"{\"notificationType\":\"Delivery\",\"mail\":{\"timestamp\":\"2016-01-01T20:00:00.000Z\",\"source\":\"something@example.com\",\"sourceArn\":\"arn:aws:ses:us-west-2:xxxx\",\"sendingAccountId\":\"xxxxxx\",\"messageId\":\"xxxx-xxxx-000000\",\"destination\":[\"someone@example.com\"]},\"delivery\":{\"timestamp\":\"2016-01-01T20:00:00.000Z\",\"processingTimeMillis\":1960,\"recipients\":[\"someone@example.com\"],\"smtpResponse\":\"250 2.0.0 OK xxxx xxx.88 - gsmtp\",\"reportingMTA\":\"axx-xx.smtp-out.us-west-2.amazonses.com\"}}", | |
"Timestamp":"2016-03-05T20:41:36.244Z", | |
"SignatureVersion":"1", | |
"Signature":"XXXX...==", | |
"SigningCertUrl":"https://sns.us-west-2.amazonaws.com/SimpleNotificationService-xxxx.pem", | |
"UnsubscribeUrl":"https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=", | |
"MessageAttributes":{} | |
} | |
} | |
] | |
} |
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
// more info: http://aaron-hoffman.blogspot.com/2016/03/store-amazon-ses-delivery-notifications.html | |
console.log("Loading function"); | |
var AWS = require('aws-sdk'); | |
//console.log(AWS.config); | |
var docClient = new AWS.DynamoDB.DocumentClient(); | |
//console.log(docClient); | |
exports.handler = function(event, context) { | |
console.log("Received event: ", JSON.stringify(event)); | |
event.Records.forEach(function(rec) { | |
//console.log("Current record: ", JSON.stringify(rec)); | |
// partition key, uuid | |
var messageId = rec.Sns.MessageId; | |
var msg = JSON.parse(rec.Sns.Message); | |
var emailAddress = msg.mail.destination[0]; | |
console.log(messageId); | |
console.log(emailAddress); | |
console.log(rec.Sns.Timestamp); | |
var params = { | |
TableName: "ses-delivery", | |
Item: { | |
messageId: messageId, | |
emailAddress: emailAddress, | |
timeStamp: rec.Sns.Timestamp, | |
snsRec: JSON.stringify(rec) | |
} | |
}; | |
//http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property | |
docClient.put(params, function(err, data) { | |
if (err) { | |
console.log("ERROR: " + err); | |
context.fail(err); | |
} | |
else { | |
console.log("SUCCESS: " + JSON.stringify(data)); | |
context.succeed(data); | |
} | |
}); | |
console.log("docClient.put() done"); | |
}); | |
}; |
blog post on how to use this: http://aaron-hoffman.blogspot.com/2016/03/store-amazon-ses-delivery-notifications.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great!