Forked from aaronhoffman/aws-sns-event-template-with-actual-ses-deliverynotification-sns-message
Created
March 19, 2016 01:20
-
-
Save tobsn/6ab06259ad726a501045 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 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
{ | |
"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\":\"[email protected]\",\"sourceArn\":\"arn:aws:ses:us-west-2:xxxx\",\"sendingAccountId\":\"xxxxxx\",\"messageId\":\"xxxx-xxxx-000000\",\"destination\":[\"[email protected]\"]},\"delivery\":{\"timestamp\":\"2016-01-01T20:00:00.000Z\",\"processingTimeMillis\":1960,\"recipients\":[\"[email protected]\"],\"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 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
// 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; | |
//console.log(rec.Sns.Message); | |
var msg = JSON.parse(rec.Sns.Message); | |
//console.log(msg.mail); | |
//console.log(msg.mail.destination); | |
//console.log(msg.mail.destination[0]); | |
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"); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment