Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active March 7, 2020 05:19
Show Gist options
  • Save diegofcornejo/14e1ffb4a6d9bea3de462a4d5b861b13 to your computer and use it in GitHub Desktop.
Save diegofcornejo/14e1ffb4a6d9bea3de462a4d5b861b13 to your computer and use it in GitHub Desktop.
Lambda - SNS - Process SES granular events and save into Cloud Firestore
  1. Download this file FirebaseAdminLayer.zip
  2. Create a Lambda Layer with the downloaded AdminFirebase.zip
  3. Attach FirebaseAdminLayer to your Lambda Function
const admin = require('firebase-admin');
let serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore();
var collection = 'aws-ses-events';
exports.handler = async(event) => {
var message = JSON.parse(event.Records[0].Sns.Message);
let doc = {
"messageId": message.mail.messageId,
"eventType": message.eventType,
"timestamp": message.mail.timestamp,
"from": message.mail.commonHeaders.from,
"to": message.mail.commonHeaders.to,
"subject": message.mail.commonHeaders.subject,
"eventInfo": message[message.eventType.toLowerCase()]
};
let promise = new Promise(function(resolve, reject) {
db.collection(collection).add(doc).then(ref => {
console.log('Added document with ID: ', ref.id);
resolve(ref.id);
});
});
return promise;
};
{
"type": "service_account",
"project_id": "randomtechguy",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment