Trying to connect to Mongo Atlas AWS VPC from Node.js application in Beanstalk of another VPC

Hi there, I have been trying to connect my Node.js application in AWS EB to my AWS Atlas cluster via peering connection but I keep running into this error: error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017. I read that it meant the Mongodb is not running but I am able to connect to it via Compass. In my Node.js application I am using the following connection string mongodb+srv://<dbuser>:<password>@noisycart-1.eoquk.mongodb.net/?retryWrites=true&w=majority

I used this quickstart guide MongoDB Atlas on the AWS Cloud, choosed the Deploy MongoDB Atlas with VPC peering into a new VPC template and some tips from this old video MongoDB (Atlas) VPC Peering with AWS Tutorial - Jay Gordon - YouTube to figure out the routing table stuff. The Atlas cluster was created in a new project and I have added the Atlas Cluster’s CIDR to the two private subnets created by the template in the host VPC. On Mongo end, I have also whitelisted the VPC CIDR and also the EC2 instance’s public IP.

What am I missing? Thanks in advance for the assistance.

After some sleep and contemplation, I realized that with a connection string, it shouldn’t be hitting the localhost. Checked my code and voila, the mongoose.connect was permanently set to the localhost. The second piece that got it working was help from AWS saying that I also need to set the routes on the public subnet that my EB instance was deployed to. I only set the routes for the private subnet. Hopefully this can help anyone following the quickstart. Cheers.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.