Nicolas Cognaux - Tech entrepreneur & engineer

I share about development, productivity, open source and entrepreneurship.

How to connect to Mongo Atlas in a VPC

2020-03-21 Nicolas Cognaux 4 min readDevelopment

At Seety, we recently switched from self-hosted Mongo cluster to Mongo Atlas, the hosted and managed MongoDB cluster. It’s a relief for me as they manage the cluster, the backups, the updates, the security, etc.

Mongo Atlas comes with great features but the one that convinced me to switch was the ability to peer the cluster directly with an existing VPC (Google, Azure or AWS). That enhances the security of the communication as everything is routed on a private network and no access is given on a public IP address. However, it also locks the developers out. Access to the DB is always interesting for maintenances or debugging.

Previously, I was simply using a port forwarding to our mongo servers in our Kubernetes cluster, but it’s not possible anymore as the Mongo Atlas cluster isn’t accessible directly. The solution I found is using SSH tunneling to a machine connected to the VPC, and I thought it can help some others!

Connect via the URL ?

To connect to the mongo atlas cluster, we use the mongodb URI (in this form):

mongodb://username:[email protected]:27017,,

This URI refers to 3 different servers (the members of the cluster):


Trying to get their IPs, will only show private IPs (only accessible from within the VPC). Here is an example of a dig output on one of the servers:

; <<>> DiG 9.11.5-P4-5.1ubuntu2.1-Ubuntu <<>>
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49806
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

; EDNS: version: 0, flags:; udp: 65494
;	IN A


;; Query time: 20 msec
;; WHEN: Sat Mar 21 14:42:21 CET 2020
;; MSG SIZE  rcvd: 91

The returned IP is (obviously not accessible from my computer). Thus, you’ll need a connexion inside your VPC to access this IP.

The SSH Tunnel

The solution to be able to connect to those local IPs is an SSH tunnel to a machine connected to the VPC. To do so, we have a VM connected to our VPC, for back-office access, only accessible from the office’s IP or via our VPN, this instance is only running an SSH server.

We create an SSH tunnel with the following command (assuming is the SSH server’s IP):

ssh -N \
  -L \
  -L \

This command actually redirects requests you make from your local machine to to (on the SSH server), to and to

We can now connect to the mongo cluster, through the SSH tunnel with the following URL (via the local IPs):

mongodb://username:[email protected]:27017,,

Invalid Hostnames

You’ll, however, receive a warning about the certificates not matching the domain names. For that, you have two possibilities, either you ignore it simply with the --tlsAllowInvalidHostnames parameter of the mongo CLI, or you edit your /etc/hosts file to resolve to etc. That’s the solution I followed and added those lines to my /etc/hosts:

A script that does it for you ?

Because I want the whole development team to be able to connect to the production and/or preproduction database cluster, I created a small script that automates the tunnel and asks for the /etc/hosts edit if not done already:


# Infos about ssh server (tunnel)
SSH_SERVER= # <== IP of your SSH server here
PORT=22 # <== Port of your SSH server here (default is 22)

# Local subnet to use (Local subnet to avoid conflicts)

# Infos about Mongo Atlas Cluster (Prod cluster)

# Check the /etc/hosts file and aask changes if needed
if grep -q $SERV0 "/etc/hosts"; then
  echo "/etc/hosts is already OK"
  echo "Be sure to have those lines in your /etc/hosts file"
  echo "$SUBNET.1       $SERV0"
  echo "$SUBNET.2       $SERV1"
  echo "$SUBNET.3       $SERV2"
  read -p "Press [Enter] when it's in /etc/hosts"

echo "Connected! Use Ctrl+C to exit."
ssh $SSH_SERVER -p $PORT -N \
    -L $SUBNET.1:27017:$SERV0:27017 \
    -L $SUBNET.2:27017:$SERV1:27017 \
    -L $SUBNET.3:27017:$SERV2:27017

echo "Disconnected!"

I hope this post will be useful for you! Don’t hesitate to give feedback and follow me on Twitter!

Don't miss an episode!

Nicolas Cognaux (Gp2mv3) - Tech entrepreneur & engineer

About Nicolas Cognaux

Entrepreneur and CTO. I’m an electrical engineer and passionate about software development. I post about technologies, entrepreneurship, projects, and geeky stuff. Follow me on social media to stay updated.

Don't miss an episode!

  • Made with ❤️ in Belgium 🇧🇪 by @Gp2mv3