I have two GCP projects(one of them is a firebase project), and I am trying access a cloud SQL instance on one project from the other project's firebase cloud functions.
I have followed the instructions here: https://cloud.google.com/sql/docs/mysql/connect-functions
On this instruction, it says
When connecting resources in two different projects, make sure that both projects have enabled the correct IAM roles and have given the service account the correct permissions.
and I am not entirely sure if I'm doing this part correct. What I did was to copy the service account email address from my cloud function project that looks like this: "service-YOUR_PROJECT_NUMBER@gcf-admin-robot.iam.gserviceaccount.com" and copied that into my other GCP project's IAM page and gave it a CloudSQL Client role.
This is the code that I am using to access the database:
import * as functions from "firebase-functions";
import * as mysql from "mysql";
export const sql = functions
.https.onRequest((request, response) => {
return new Promise<any>(async (resolve, reject) => {
const query = "select * from my_table limit 1;";
const pool: mysql.Pool = mysql.createPool({
socketPath: functions.config().cloud_sql.socket_path,
user: functions.config().cloud_sql.user,
password: functions.config().cloud_sql.password,
database: functions.config().cloud_sql.database,
connectionLimit: 5,
timezone: "+0900"
});
pool.query(query, [], (err: mysql.MysqlError | null, results: any) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(results);
}
});
});
});
When I access this function, I get this error in the console:
Error: connect ECONNREFUSED
What could I be missing? Any help is greatly appreciated.