1
votes

I am testing a nodejs app on GCP functions and a MongoDB instance in GCP cloud run(I know this is not a good idea) but again this is a test. I am able to get the mongo image running on cloud run and I am given a service endpoint as https://mongodb.foo.bar.run.app and I have the container port in this cloud run service as 27017. When I try and edit the mongoose.connect() method to include this service uri I am not able to connect with the app. I have the mongodb cloud run instance open to receive traffic from all traffic(Again not a good idea), but I still can't get the node app to connect.

I have setup the connection in node as mongoose.connect('mongodb://mongodb.foo.bar.run.app:27017/test');

but I get this error

Error: connect ETIMEDOUT <IP ADDRESS GOES HERE>:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    at TCPConnectWrap.callbackTrampoline (internal/async_hooks.js:129:14)
Emitted 'error' event on NativeConnection instance at:
    at NativeConnection.Connection.error (/Users/foo/nodejs/bar/node_modules/mongoose/lib/connection.js:443:8)
    at /Users/foo/nodejs/bar/node_modules/mongoose/lib/connection.js:472:15
    at /Users/foo/nodejs/bar/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:59:21
    at /Users/foo/nodejs/bar/node_modules/mongodb/lib/db.js:232:14
    at Server.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb/lib/server.js:240:9)
    at Object.onceWrapper (events.js:421:26)
    at Server.emit (events.js:314:20)
    at Pool.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/topologies/server.js:308:68)
    at Pool.emit (events.js:314:20)
    at Connection.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/connection/pool.js:115:12)
    at Object.onceWrapper (events.js:421:26)
    at Connection.emit (events.js:314:20)
    at Socket.<anonymous> (/Users/foo/nodejs/bar/node_modules/mongodb-core/lib/connection/connection.js:144:49)
    at Object.onceWrapper (events.js:421:26)
    at Socket.emit (events.js:314:20)
    at emitErrorNT (internal/streams/destroy.js:100:8) {
  name: 'MongoError'
}

I tried to curl the service uri as curl https://mongodb.foo.bar.run.app and I get a 200 response in the mongo logs GET 200 466B but if I curl mongodb.foo.bar.run.app I get a 302 response code GET 302 0B.

I looked at the mongoose documentation but I couldn't figure what I was doing wrong. I know the documentation states user and password on the uri string, but when I run it locally on my docker engine I did not use a user and password and it worked.

1
It can't work, it's not a HTTP protocol, but TCP protocol!! You can use GKE autopilot to host your databaseguillaume blaquiere
It is weird that I was able to spin up the same db in AWS fargate and it worked. I didn't know about GKE autopilot, thanks.user12081440

1 Answers

2
votes

As mentioned in the comments, it's not possible to run MongoDB on Cloud Run since as mentioned here:

Cloud Run can only receive HTTP requests or Pub/Sub push events.

Connecting to MongoDB requires a TCP protocol rather than HTTP, that's why every call to it would fail even if the port is reachable.

Cloud Run is in this sense more similar to AWS Lambda than it's to AWS Fargate as it's even billed on a per request basis rather than on resources consumed.