How to connect to MongoDB cluster from AWS Lambda?
A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability and are the basis for all production deployments.
The replica set connection string looks something like this,
mongodb://xx.x.xx.xx:27017,xx.x.xx.xxx:27017,xx.x.xx.xx:27017/db_name?rs=rs0
This is used like the normal mongo connection string where ever needed. But the change that we also make is to add the following entries in the /etc/hosts file as follows:
172.xx.xx.xx mongo-master
172.xx.xx.xx mongo-1
172.xx.xx.xx mongo-2
Its simple to add them to servers you are aware of and have SSH rights. But for Lambda this rule doesn't fit. But the primary concept of Lambda i.e serverless is that there are no servers involved.
It doesn't practically mean that there are no servers, but at the end of the day, we have no idea as to where our code gets deployed.
So how do we do this in Lambda, well it’s just mere 3-4 magical lines,
var fs = require(‘fs’);
var to_append = process.env.HOSTNAMES;
fs.appendFile(process.env.HOSTALIASES,to_append , function(err){
if(err) throw err;
});
Include these lines, in each of your function and you are good to go.
Environment values are :
HOSTNAMES: “mongo-master 172.xx.xx.xx\n mongo-1 172.xx.xx.xx \n mongo-2 172.xx.xx.xx \n”
HOSTALIASES: “/tmp/HOSTALIASES”
So, appendFile function will create a file named “HOSTALISASES” if it doesn't exist and also add the hostname entries to it. So now the MongoDB will be able to resolve what mongo-master actually means.
Please get in touch in case of any queries.
Thanks.