node.jsredisgoogle-cloud-run

Can't connect to redis bull from google cloud run


I'm trying to setup my redis bull on Google Cloud run. Locally everything works.

I use the following code when the server starts:

const client = redis.createClient('6379', '10.103.YYY.YYY');

This seems to be working. I don't receive any error on startup.

When I try to launch a job with Bull like:

             const actionQueue = new Bull(uuid(), {
                redis: {
                    port: 6379, host: '10.103.YYY.YYY', tls: {
                        servername: '10.103.YYY.YYY'
                    }
                }
            });
         
            await actionQueue.add();

            actionQueue.process(async (job) => {
                return this._job();
            });

            actionQueue.on('completed', async (job, actionId) => {
                console.log(`Job completed with result ${actionId}`);
            });

            actionQueue.on("failed", (job, error) => {
                console.log(job.id, error);
            });

But the redis is still connecting to localhost ip&port. Anyone an idea why this is still connecting to my localhost? Do I need to setup on different way?

Error log:

2020-11-21T14:55:35.794783Z <rejected> Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794791Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.794798Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794804Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794810Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.794816Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.794822Z port: 6379
Standaard
2020-11-21T14:55:35.794828Z }
Standaard
2020-11-21T14:55:35.794834Z}
Standaard
2020-11-21T14:55:35.794987Z The error was: Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794994Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.795Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795006Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795011Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.795017Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.795022Z port: 6379
Standaard
2020-11-21T14:55:35.795028Z}

Solution

  • Cloud Run doesn't speak to the VPC natively. You need to bridge Serverless world with out project VPC.

    For this, you have to use serverless VPC connector. Create on in the same region as your Cloud Run is deployed.

    Then attach it to your Cloud Run service. Like this, the private range (at least, you can also route all the outgoing traffic) traffic is routed to the connector and lands on your VPC. Now you can access to private resources in your VPC like your redis instance.