I have setup a MongoDB Atlas. Also, I have configured a VPC peering from MongoDB Atlas to my AWS VPC. I have allowed VPC CIDR in the IP access list on MongoDB Atlas and it's working fine.
I also have a client VPN endpoint that I am using to connect AWS VPC via secure tunnel. I have also allowed my client VPN private IP range on MongoDB Atlas IP access list but I am unable to connect to MongoDB Atlas endpoint.
I tried to dig down further and performed these steps:
resolve hostname from SRV record:
nslookup -type=SRV _mongodb._tcp.clusterx.xxxxxxxxxx.mongodb.net
ping the hostname
ping clusterx-shard-xx-xx.xxxxxx.mongodb.net
When trying the ping from AWS VPC, it resolves to the private IP of that hostname but when I try to ping the hostname from local, it resolves to public IP of that hostname. That was the reason I was unable to connect to MongoDB Atlas endpoint from my local.
Is there any way to make MongoDB connection work with private IP from my local?
You're most probably missing the route to the Atlas instance within your VPN connection.
AWS is aware of the destination IP address (therefore, it is resolvable), but doesn't route your traffic to it (therefore, ping fails).
To resolve, you need to add the route to the Atlas instance - or add a default route that brings all the traffic through your VPN (which I do not recommend). You can find out how to do it in more detail in AWS documentation.