I have an API running on aws lambda and api gateway. On the local host and the test database (mlab) everything works fine. But when deploying to Amazon my API cannot add data to mongodb which is on ec2. We have all the permissions open and lambda and mongodb are on the same vpc.

Here is my code:

MongoClient.connect(dbURL, {useNewUrlParser: true }, function (err, db){ if (err) { console.log('Unable to connect to the mongoDB server. Error: ', err); } else { console.log('Connection established to ', dbURL); } let myDB = db.db('iyc') let arr = []; locations.forEach(element => { let insertData = {}; insertData.user_id = req.body.user_id; insertData.device_id = req.body.device_id; insertData.app_type = req.body.app_type; insertData.datetime = JSON.parse(element.datetime)/1000; insertData.latitude = element.latitude; insertData.longitude = element.longitude; insertData.address = element.address; insertData.method = element.method; insertData.accuracy = element.accuracy; insertData.status = element.status; insertData.confidence = element.confidence arr.push(insertData) }) try { myDB.collection('userdevices').insertMany(arr, function(err, data) { if(err) { console.log("Insert db err " + err) } else if(!data) { console.log('No data has been inserted') } }) } catch(err) { console.log(err) } db.close(); }) 
  • I do not understand, and here the switch functions when it comes to the database? See what prints in error. But the arrow functions are only a feature of the JavaScript language, no more. - And
  • What does Lambda end up with? (You can see in CloudWatch). What "all" permissions are open, what is the configuration of the sercurity group? - Sergey Kovalev
  • Unable to connect to the mongoDB server. Error: {MongoNetworkError: failed to connect to server [172.31.102.145:27017] Sysadmin says that I opened the permission to me so that I had access to db - Sargsyan Miqayel
  • By mistake, are you catching a timeout on a connection, maybe a database is being loaded, or is it not connecting there? - Klimenkomud
  • Checked the host port username and password. The database also does not work as it is our test (qa) - Sargsyan Miqayel

0