google-cloud-platformroutermongodb-atlasvpc

Cannot Connect to MongoDB Atlas from GCP Cloud Run Instance


I followed this guide to creating a static outbound IP and attaching it to the instance. I have confirmed that the outbound IP is correctly setup and that outbound requests are from that IP by using this quick snippet:

const response = await axios.get('https://ipinfo.io/ip');
const externalIP = response.data.trim();
console.log(`Outbound External IP Address: ${externalIP}`);

I also confirmed that the logged IP is exactly the same as the IP whitelisted for the database. However, I'm still unable to connect. As with Mongoose, there aren't very descriptive errors for connection errors, so I'm just getting the SSL handshake failed error. Any ideas?

EDIT 1:

Here is how I am connecting to the cluster:

const mongoose = require('mongoose');

const uri = process.env.MONGODB_URI;

let connection;
const connect = async () => {
  try {
    connection = await mongoose.connect(uri, {
      bufferCommands: false, // Disable mongoose buffering
    });
    return connection;
  } catch (e) {
    console.error("Could not connect to MongoDB...");
    throw e;
  }
};

function getConnection() {
  return connection;
}

module.exports = { connect, getConnection };

And here is the error:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist

error: MongoNetworkError: 08123BD0DA3E0000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1590:SSL alert number 80

Solution

  • Hi this is just another suggestion.

    You can attempt to establish a connection by utilizing private IP access from Cloud Run to the MongoDB Atlas cluster. This configuration ensures that MongoDB connects exclusively through private IP addresses. This configuration will use VPC peering.

    First you need to create a VPC serverless access connector, make sure when you create this, your region should match the serverless service. Use a custom IP range and configure a private IP range with /28 mask.

    Note that this configuration in MongoDB Atlas cluster uses Dedicated Cluster for VPC network peering. Under MongoDB atlas access list, in the security section click Network access then add IP address. In the Access list entry, you may enter the /28 private ip address that you created under the VPC access connector.

    Next is to configure the MongoDB Atlas VPC peering. You may use this documentation for guidelines.

    To peer, the next is to create VPC peering on GCP. Enter the project ID and VPC network name that were provided to configured MongoDB Atlas VPC peering.

    Below is a screenshot of the next step that is needed to do. For a detailed instruction of this suggestion you may visit this link

    image