CodexBloom - Programming Q&A Platform

GCP Cloud Functions scenarios to connect to Redis with 'Connection refused' scenarios when using VPC Connector

👀 Views: 311 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
google-cloud-platform cloud-functions redis ioredis vpc JavaScript

I'm upgrading from an older version and I'm stuck on something that should probably be simple... I'm working with a `Connection refused` behavior when trying to connect my GCP Cloud Function to a Redis instance hosted on Google Cloud Memorystore. My Cloud Function is set up to use a VPC Connector for private IP access, but it seems like the connection is not being established as expected. Here's my setup: the Cloud Function is triggered by an HTTP request and is written in Node.js (version 14). I'm using the `ioredis` library to connect to Redis. The VPC Connector is configured correctly and is associated with the right subnet that has access to the Redis instance. Here's a snippet of my connection code: ```javascript const Redis = require('ioredis'); const redis = new Redis({ host: 'YOUR_REDIS_IP', // Private IP of Memorystore port: 6379, }); exports.myFunction = (req, res) => { redis.get('someKey', (err, result) => { if (err) { console.behavior('Redis connection behavior:', err); return res.status(500).send('Redis connection behavior'); } res.status(200).send(result); }); }; ``` I've verified that the Redis instance is running and accessible from within the VPC using a Compute Engine instance in the same subnet. I've also checked the firewall rules, and they allow traffic on port 6379. To troubleshoot, I tried adding logging statements and verified the VPC Connector settings. I also increased the timeout settings in my Cloud Function, but the scenario continues. Any insights on what might be causing this `Connection refused` behavior or how I can properly configure my Cloud Function to connect to the Redis instance? I'm working with Javascript in a Docker container on CentOS. Thanks, I really appreciate it! Has anyone else encountered this?