Skip to content

Instantly share code, notes, and snippets.

@alindgren
Last active April 11, 2018 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alindgren/400617e713dbb174f52cb0f0e8b8c6ea to your computer and use it in GitHub Desktop.
Save alindgren/400617e713dbb174f52cb0f0e8b8c6ea to your computer and use it in GitHub Desktop.
Fundraise Airtable integration using SQS and Lambda - see https://www.alexlindgren.com/posts/asynchronous-microservices-with-aws-sqs-and-aws-lambda/
let AWS = require('aws-sdk');
let sqs = new AWS.SQS();
let Airtable = require('airtable');
let base = new Airtable({apiKey: 'keyql7soeJgu7cdsP'}).base('appJdKhgVuGZUv0va');
exports.handler = (event, context, callback) => {
sqs.receiveMessage({
QueueUrl: 'https://sqs.us-west-2.amazonaws.com/852229429830/FundraiseDonations',
AttributeNames: ['All'],
MaxNumberOfMessages: '10',
VisibilityTimeout: '30',
WaitTimeSeconds: '20'
}).promise()
.then(data => {
data.Messages.forEach(message => { // iterate through all fetched messages
let messageBody = JSON.parse(message.Body);
console.log("FundraiserId: " + messageBody.FundraiserId);
base('Donations').create({
"Id": messageBody.Id,
"Creation Date": messageBody.CreationDate,
"Donor Id": messageBody.UserId,
"Fundraiser Id": messageBody.FundraiserId,
"Donation Amount": messageBody.DonationAmount,
"Donor Display Name": messageBody.DonorDisplayName
}, function(err, record) {
if (err) { console.error(err); return; }
console.log(record.getId());
});
sqs.deleteMessage({ // Delete message
QueueUrl: "https://sqs.us-west-2.amazonaws.com/852229429830/FundraiseDonations",
ReceiptHandle: message.ReceiptHandle
}).promise()
.then(data => {
console.log("Successfully deleted message with ReceiptHandle : " + message.ReceiptHandle +
"and booking reference : " + messageBody.bookingRef + " with response :" + JSON.stringify(data));
})
.catch(err => {
console.log("Error while deleting the fetched message with ReceiptHandle : " + message.ReceiptHandle +
"and booking reference : " + messageBody.bookingRef, err);
});
});
})
.catch(err => {
console.log("Error while fetching messages from the sqs queue", err);
});
callback(null, 'Lambda execution completed');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment