I have a mongoDB atlas for application. I have connected to atlas from local env by whitelisting my IP on MongoDB atlas also, I have whitelisted my GCP VPC CIDR and mongo connector as like below
I have the following code return for GCP cloud function
const co = require('co');
const mongodb = require('mongodb');
const uri = "mongodb+srv://admin:<PASSWORD>@uat-ca.sozdm.mongodb.net/<DATABASE>?retryWrites=true&w=majority";
exports.helloWorld = (req, res) => {
co(function*() {
const client = yield mongodb.MongoClient.connect(uri);
const docs = yield client.db('automation').collection('test').find().toArray();
console.log(docs)
res.send('Result: ' + JSON.stringify(docs));
}).catch(error => {
console.log(error.toString())
res.send('Error: ' + error.toString());
});
};
I have tested this code on local and it's working fine but whenever I'm trying to run this code on cloud function I'm getting below error even I have added cloud function under same VPC :
MongoServerSelectionError: Server selection timed out after 30000 ms at Timeout
I have gone through multiple posts like stackoverflow Mongo Community and tried multiple ways but was not been able to solve the problem.
Appreciate your help.