Trying to connect to Azure CosmoDB but receiving: MongooseServerSelectionError: Server selection timed out


I am having a connection issue to Azure CosmoDB from my Netlify application and I am struggling to identify the cause.

Netlify site name:

Build settings:

  • Runtime: Next.js (v5)
  • Base directory: /
  • Build command: pnpm run build (which run next build)
  • Publish directory: .next

Environment Variables:

MONGODB_URI: mongodb://<credentials>

This URI was generated by CosmoDB

Tech stack:

Node: v20.15.1
pnpm: v8
Next: v14.2.4
Mongoose: v8.4.4

When running the build I am receiving the following error:

10:39:01 AM: MongooseServerSelectionError: Server selection timed out after 30000 ms
10:39:01 AM:     at _handleConnectionErrors (/opt/build/repo/node_modules/.pnpm/mongoose@8.4.4/node_modules/mongoose/lib/connection.js:897:11)
10:39:01 AM:     at NativeConnection.openUri (/opt/build/repo/node_modules/.pnpm/mongoose@8.4.4/node_modules/mongoose/lib/connection.js:848:11)
10:39:01 AM:     at async l (/opt/build/repo/.next/server/app/page.js:1:2766)
10:39:01 AM:     at async u (/opt/build/repo/.next/server/app/page.js:1:2957) {
10:39:01 AM:   reason: TopologyDescription {
10:39:01 AM:     type: 'ReplicaSetNoPrimary',
10:39:01 AM:     servers: Map(1) {
10:39:01 AM:       '<server_url>:10255' => [ServerDescription]
10:39:01 AM:     },
10:39:01 AM:     stale: false,
10:39:01 AM:     compatible: true,
10:39:01 AM:     heartbeatFrequencyMS: 10000,
10:39:01 AM:     localThresholdMS: 15,
10:39:01 AM:     setName: 'globaldb',
10:39:01 AM:     maxElectionId: null,
10:39:01 AM:     maxSetVersion: null,
10:39:01 AM:     commonWireVersion: 0,
10:39:01 AM:     logicalSessionTimeoutMinutes: null
10:39:01 AM:   },
10:39:01 AM:   code: undefined
10:39:01 AM: }

I have setup Azure Cosmos DB for MongoDB using server version 6.0. In my application, I am using Mongoose to connect to the server. I have the server access set to public on Azure and am able to connect to the server both locally and on other platforms like Vercel. It seems like the issues is specific to my Netlify deployment.

The CosmoDB instance firewall settings are set to:

Public network access: All networks, including the internet, can access this Azure Cosmos DB account.

I have tried:

Updating the connection string to include the following params:

family=4 - this forces usage of ipv4

I have also included options in the mongoose code to:

  serverSelectionTimeoutMS: 30000, // 30 seconds
  socketTimeoutMS: 45000, // 45 seconds
  ssl: true,
  replicaSet: 'globaldb',

Here is my mongoose.ts file being used to connect:

import mongoose from 'mongoose';

const MONGODB_URI: string | undefined = process.env.MONGODB_URI;

  throw new Error('MONGODB_URI is not defined');

const options = {
  serverSelectionTimeoutMS: 30000, // 30 seconds
  socketTimeoutMS: 45000, // 45 seconds
  ssl: true,
  replicaSet: 'globaldb',

const connect = async (): Promise<typeof mongoose> => {
  try {
    return await mongoose.connect(MONGODB_URI, options);
  } catch (err) {

export default connect;

I have added some network checks to my build command, specifically:

"build": "echo 'Environment variables:' && env | sort && echo 'Resolving CosmosDB domain:' && nslookup <cluster_url> && echo 'Testing connection to CosmosDB:' && curl -v <cluster_url>:443 && next build"

Which have shown to be resolving correctly.

I have also tried changing the Next runtime to v4, version 18 and 16 of node, and removing all mongo db code to ensure the site build is otherwise working, which it is.

I have tested the connection using the mongo shell which is working fine too.

I have setup a mongodb cluster using Atlas and was able to successfully connect to the cluster and read/write from the database which means this is specifically a connection issue between Cosmo and Netlify. As mentioned earlier, this connection to Cosmo works on other cloud services like Vercel.

I am not sure how to debug this issue further or resolve the connection timeout/error shown at the top of this post. Are there any known issues with connecting to CosmoDB from Netlify?

Any help with this would be appreciated.


Kees Jansen

Do you have any connection logs on your end? Not sure how much we can help here given that the timeout is not coming from Netlify, but from a 3rd party server that we don’t control.

Have you checked on their end for any blocks or restrictions?