Last active
February 22, 2021 20:00
-
-
Save realFranco/4b623b538f7e6e1961d34eecadf8a616 to your computer and use it in GitHub Desktop.
Code for a Lambda function. Send data into Dynamodb.
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
/** | |
* Date: June 16th, 2020 | |
* Dev: franco@systemagency.com | |
* | |
* > Code triggered by the lambda function to continue with SES Stats. | |
* > Including the original email headers on delivery notifications. | |
* | |
* Code taken from: | |
* > https://aws.amazon.com/premiumsupport/knowledge-center/lambda-sns-ses-dynamodb/ | |
* | |
* Ref. notification examples: | |
* > https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-examples.html | |
*/ | |
console.log('Loading event'); | |
const aws = require('aws-sdk'); | |
const { SES } = require('aws-sdk'); | |
const ddb = new aws.DynamoDB({params: {TableName: 'SESNotifications'}}); | |
exports.handler = function(event, context){ | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
var rowToDynamo = false, | |
commonHeaders = ''; | |
var SnsPublishTime = event.Records[0].Sns.Timestamp; | |
var SESMessage = event.Records[0].Sns.Message; | |
// parse the data incomming from Sns.Message | |
SESMessage = JSON.parse(SESMessage); | |
var SESMessageId = SESMessage.mail.messageId; | |
var SESMessageType = SESMessage.notificationType, | |
SESDestinationAddress = SESMessage.mail.destination.toString(); | |
if (SESMessageType == 'Bounce'){ | |
var SESreportingMTA = SESMessage.bounce.reportingMTA; | |
var SESbounceSummary = JSON.stringify( | |
SESMessage.bounce.bouncedRecipients); | |
commonHeaders = (typeof(SESMessage.mail.commonHeaders) != 'string') | |
? JSON.stringify(SESMessage.mail.commonHeaders) | |
: SESMessage.mail.commonHeaders; | |
rowToDynamo = { | |
Item: { | |
SESMessageId : {S: SESMessageId}, | |
SnsPublishTime : {S: SnsPublishTime}, | |
SESreportingMTA : {S: SESreportingMTA}, | |
SESDestinationAddress : {S: SESDestinationAddress}, | |
SESbounceSummary : {S: SESbounceSummary}, | |
SESMessageType : {S: SESMessageType}, | |
SESCommonHeaders : {S: commonHeaders} | |
}}; | |
} | |
else if (SESMessageType == 'Delivery'){ | |
var SESsmtpResponse1 = SESMessage.delivery.smtpResponse, | |
SESreportingMTA1 = SESMessage.delivery.reportingMTA; | |
commonHeaders = JSON.stringify( | |
SESMessage.mail.commonHeaders) || 'CommonHeaders-Empty'; | |
rowToDynamo = { | |
Item: { | |
SESMessageId : {S: SESMessageId}, | |
SnsPublishTime : {S: SnsPublishTime}, | |
SESsmtpResponse : {S: SESsmtpResponse1}, | |
SESreportingMTA : {S: SESreportingMTA1}, | |
SESDestinationAddress : {S: SESDestinationAddress}, | |
SESMessageType : {S: SESMessageType}, | |
SESCommonHeaders : {S: commonHeaders} | |
} | |
}; | |
} | |
else if (SESMessageType == 'Complaint'){ | |
var SESComplaintFeedbackType = SESMessage.complaint.complaintFeedbackType, | |
SESFeedbackId = SESMessage.complaint.feedbackId; | |
commonHeaders = JSON.stringify( | |
SESMessage.mail.commonHeaders) || 'CommonHeaders-Empty'; | |
rowToDynamo = { | |
Item: { | |
SESMessageId : {S: SESMessageId}, | |
SnsPublishTime : {S: SnsPublishTime}, | |
SESComplaintFeedbackType : {S: SESComplaintFeedbackType}, | |
SESFeedbackId : {S: SESFeedbackId}, | |
SESDestinationAddress : {S: SESDestinationAddress }, | |
SESMessageType : {S: SESMessageType}, | |
SESCommonHeaders : {S: commonHeaders} | |
}}; | |
} | |
if(rowToDynamo){ | |
ddb.putItem(rowToDynamo, function(err, data){ | |
if(err) { context.fail(err)} | |
else { | |
console.log(data); | |
context.succeed(); | |
} | |
}); | |
} | |
console.log('End Lambda Trigger'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{
"errorType": "SyntaxError",
"errorMessage": "Unexpected token e in JSON at position 0",
"trace": [
"SyntaxError: Unexpected token e in JSON at position 0",
" at JSON.parse ()",
" at Runtime.exports.handler (/var/task/index.js:30:23)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}