Skip to content

Instantly share code, notes, and snippets.

View raphaellondner-mongodb's full-sized avatar

raphaellondner-mongodb

View GitHub Profile
@raphaellondner-mongodb
raphaellondner-mongodb / mongodb-atlas-lambda-local-1
Created March 8, 2017 20:38
Lambda-local call with MongoDB Atlas
lambda-local -l app.js -e event.json -E {\"MONGODB_ATLAS_CLUSTER_URI\":\"mongodb://lambdauser:$PASSWORD@lambdademo-shard-00-00-7xh42.mongodb.net:27017\,lambdademo-shard-00-01-7xh42.mongodb.net:27017\,lambdademo-shard-00-02-7xh42.mongodb.net:27017/$DATABASE?ssl=true\&replicaSet=lambdademo-shard-0\&authSource=admin\"}
@raphaellondner-mongodb
raphaellondner-mongodb / mongodb-atlas-lambda-step0.js
Last active March 29, 2017 22:00
mongodb-atlas-lambda-step0
'use strict'
var MongoClient = require('mongodb').MongoClient;
let atlas_connection_uri;
let cachedDb = null;
exports.handler = (event, context, callback) => {
var uri = process.env['MONGODB_ATLAS_CLUSTER_URI'];
@raphaellondner-mongodb
raphaellondner-mongodb / mongodb-atlas-lambda-step2.js
Last active February 6, 2018 13:53
AWS KMS code to decrypt the encrypted connection string
const kms = new AWS.KMS();
kms.decrypt({ CiphertextBlob: new Buffer(uri, 'base64') }, (err, data) => {
if (err) {
console.log('Decrypt error:', err);
return callback(err);
}
atlas_connection_uri = data.Plaintext.toString('ascii');
processEvent(event, context, callback);
});
function processEvent(event, context, callback) {
console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event));
var jsonContents = JSON.parse(JSON.stringify(event));
//date conversion for grades array
if(jsonContents.grades != null) {
for(var i = 0, len=jsonContents.grades.length; i < len; i++) {
//use the following line if you want to preserve the original dates
//jsonContents.grades[i].date = new Date(jsonContents.grades[i].date);
@raphaellondner-mongodb
raphaellondner-mongodb / lambda-mongodb-example
Last active March 11, 2020 02:37
MongoDB-Lambda-Example
'use strict'
const AWS = require('aws-sdk');
var MongoClient = require('mongodb').MongoClient;
let atlas_connection_uri;
exports.handler = (event, context, callback) => {
var uri = process.env['MONGODB_ATLAS_CLUSTER_URI'];
if (atlas_connection_uri != null) {