Connect NestJS API in Cloud Run with Cloud SQL db

I have a NestJs API triying to deploy to Cloud Run but it fails at the moment it tries to connect to Cloud SQL. i'm using typeORM.

This is the config to connect the DB

import { Client, ClientConfig } from 'pg';

let dbConfig: ClientConfig = {
  host: process.env.DB_HOST,
  port: +process.env.DB_PORT,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
};

const client = new Client(dbConfig);

tried to pass diferent "host"

This is the error iam getting if using the Cloud SQL Connection name Error: connectToServer: Error: getaddrinfo EAI_AGAIN project-name:us-central1:db-name

This is the error iam getting if using the Cloud SQL Public IP address Error: connectToServer: Error: connect ETIMEDOUT xxx.xxx.xxx.xxx:5432

The Cloud Run have a service account with Cloud SQL client roles In Cloud SQL connections i selected my SQL instance I am explicity passing this vars in cloud run config

I expect to connect to the DB as normal as i connect when iam on localhost

Cloud SQL is set as Public because i want to connect from localhost and also test from Cloud Run

Upvotes: -1

Views: 296

Answers (1)

Paku
Paku

Reputation: 797

Clicking "Public IP" just assigns a public IP to the DB. It does not make networking resources needed to address the DB directly from public internet.

"Public IP" allows you to use Cloud SQL Proxy to connect to the DB. This is what you should use.

If you go to GCP UI. Click Connections -> Security, you will see this: enter image description here

Upvotes: 0

Related Questions