Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active March 7, 2020 21:46
Show Gist options
  • Save diegofcornejo/562c04fb17bc51a3cd3238050e889774 to your computer and use it in GitHub Desktop.
Save diegofcornejo/562c04fb17bc51a3cd3238050e889774 to your computer and use it in GitHub Desktop.
Express - SNS - Process SES granular events and save into Cloud Firestore

This assume you already have a SNS subscription (HTTP endpoint) and configured SES events, if not please go to this link

  1. Replace content from serviceAccountKey.json with your own info.

  2. Install dependencies

npm install --save express firebase-admin
  1. Start App
node index.js
var express = require('express');
var app = express();
const admin = require('firebase-admin');
let serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
app.use(
express.json({
type: [
'application/json',
'text/plain', // AWS sends this content-type for its messages/notifications
],
})
)
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.set('port', process.env.PORT || 4000);
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port);
});
let db = admin.firestore();
var collection = 'aws-ses-events';
app.post('/sns', function(req, res) {
console.log(JSON.parse(req.body.Message));
var message = JSON.parse(req.body.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()]
};
db.collection(collection).add(doc).then(ref => {
console.log('Added document with ID: ', ref.id);
res.send(ref.id);
});
});
{
"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