Atlas connection socket timeout error from Google cloud functions

We are trying to connect to atlas from google cloud functions which throw this error:

MongoNetworkError: failed to connect to server [<mongodb_host>:27017] on first connect [MongoNetworkError: connection timed out
    at connectionFailureError (/workspace/node_modules/mongodb-core/lib/connection/connect.js:362:14)
    at TLSSocket.<anonymous> (/workspace/node_modules/mongodb-core/lib/connection/connect.js:286:16)
    at Object.onceWrapper (events.js:420:28)
    at TLSSocket.emit (events.js:314:20)
    at TLSSocket.EventEmitter.emit (domain.js:483:12)
    at TLSSocket.Socket._onTimeout (net.js:483:8)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {

Sometimes it connect and sometimes it fails. Let us know the possible cause of this.

Prateek what kind of network IP Access List or private networking setup are you using?

Thanks Andrew for replying. We are using VPC Network for Egress settings in google cloud functions.

Got it, are you connecting to a dedicated Atlas cluster (M10+) witth VPC peering?

Yes we are connecting to the dedicated Atlas cluster.