Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the Lambda function for taking SNS messages from SES and saving them into a DynamoDB table.
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({
params: {
TableName: process.env.ddb_table_name
}
});
exports.handler = function(event, context, callback) {
var result = true;
for (var i=0, len = event.Records.length; i < len; i++) {
saveToDynamoDB(event.Records[i], context);
}
};
function saveToDynamoDB(event, context) {
var sns_event = event.Sns;
var msg = JSON.parse(sns_event.Message);
var ddb_item = {
Item: {
sender: {"S": msg.mail.source},
sns_message_id: {"S": sns_event.MessageId},
message: {"S": sns_event.Message}
},
TableName: process.env.ddb_table_name
};
var result = ddb.putItem(ddb_item, function(err, data) {
if (err) {
console.log("Error in putItem: " + err.message + " (" + err.code + ")");
context.fail(err);
}
else {
console.log("Success");
context.succeed();
}
});
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment