Connect to Cloud SQL from Google Functions using private IP


RmR

I have a Google Cloud SQL instance and plan to access it from a Google Function using NodeJS. The code is as described in the documentation

// mysql
const mysql = require('mysql');
const connectionName = process.env.INSTANCE_CONNECTION_NAME || 'project:region:sql-instance';
const dbUser = process.env.SQL_USER || 'root';
const dbPassword = process.env.SQL_PASSWORD || 'password';
const dbName = process.env.SQL_NAME || 'database';

const mysqlConfig = {
  connectionLimit: 1,
  user: dbUser,
  password: dbPassword,
  database: dbName,
};
//if (process.env.NODE_ENV === 'production') {
  mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
//}

// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;

function searchDB() {
  if (!mysqlPool) {
    mysqlPool = mysql.createPool(mysqlConfig);
    console.log(mysqlPool);
  }
  return new Promise( function( resolve, reject ) {
    mysqlPool.query('SELECT * FROM table', (err, results) => {
      if (err) {
        console.error('error in retrieving data:', err);
        resolve(''); // sending blank response
      } else {
        console.log('success in getting data', JSON.stringify(results));
        resolve(JSON.stringify(results));
      }
    });
  });
}

Once the database is set up with the public IP, I am able to access the database. However, when I set it to "Dedicated IP", I get an ECON refusederror.

The question is: what changes do I need to make to the Google Functions code to access it using the private IP? thanks

Kurtif

Private IPs can only be accessed by other services on the same Virtual Private Cloud (VPC) . Since your function is running on a managed network, it is currently outside the VPC and does not have access to the Cloud SQL instance.

Google Cloud Functions does provide a unix domain socket to interface with the Cloud SQL instance, but it requires the instance to have a public IP. As long as you mysqlConfig.socketPath = cloudsql/${connectionName};use the correct connection name (in the format project:region:instance), you should be able to connect. If you use cross-product or cross-regional settings, learn more here .

Update: Since the original posting of this content, a new product called the Serverless VPC Connector has been made available to allow access to VPC resources. You can follow the instructions to configure and then connect to the instance using its private IP (instead of the socket mentioned above ) ./cloudsql/

Related


Connect to Cloud SQL from Google Functions using private IP

RmR I have a Google Cloud SQL instance and plan to access it from a Google Function using NodeJS. The code is as described in the documentation // mysql const mysql = require('mysql'); const connectionName = process.env.INSTANCE_CONNECTION_NAME || 'project:reg

How to connect Google Cloud SQL from Cloud Functions?

Widely distributed from: I'm trying to build an API that communicates with a Google Cloud SQL (PostgreSQL) instance using Cloud Functions for Firebase . I am using HTTP(S) triggers. When I whitelist my desktop's IP address, I can connect to Cloud SQL using the

How to connect Google Cloud SQL from Cloud Functions?

Widely distributed from: I'm trying to build an API that communicates with a Google Cloud SQL (PostgreSQL) instance using Cloud Functions for Firebase . I am using HTTP(S) triggers. When I whitelist my desktop's IP address, I can connect to Cloud SQL using the

Connect to Google Cloud Sql from metabase cloud

Shahram I have a google cloud Sql Instance. I just created a metabase account on my metabase website cloud account and want to add a data source. But can't connect when I enter my database credentials. It shows that I have to add the metabase to the google clo

SSL-only Google Cloud SQL private IP

Ruslan Muhamadiarov: I want to connect to Cloud Sql from spring boot application in GCP using private ip and ssl, only for public ip. And I don't understand "SSL only token" only for public connections or public/private? Gabe Weiss: They are two different aspe

SSL-only Google Cloud SQL private IP

Ruslan Muhamadiarov: I want to connect to Cloud Sql from spring boot application in GCP using private ip and ssl, only for public ip. And I don't understand "SSL only token" only for public connections or public/private? Gabe Weiss: They are two different aspe