I am unable to connect to the Cloud SQL inside a VPC via Cloud Run Service. The same code which was running for years is not working now.
My account was suspended but it got restored. The team suspended because they found some false event which was not true and they brought it back within 1 week. But after this my services were down and when I re-deployed the same code they are unable to connect.
Code -
dialer, err := cloudsqlconn.NewDialer(ctx, cloudsqlconn.WithIAMAuthN(), cloudsqlconn.WithLazyRefresh())
if err != nil {
return nil, fmt.Errorf("failed to create Cloud SQL dialer: %w", err)
}
dsn := buildDSN(config)
pgxConfig, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("failed to parse Cloud SQL DSN: %w", err)
}
var opts []cloudsqlconn.DialOption
if config.UsePrivateIP {
opts = append(opts, cloudsqlconn.WithPrivateIP())
}
pgxConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(ctx, config.CloudSQLInstanceConnectionName, opts...)
}
db := stdlib.OpenDB(*pgxConfig)
Error -
"Database connection failed after 5 attempts: failed to ping Cloud SQL: failed to connect to <service-account> database:test: /tmp/.s.PGSQL.5432 (/tmp): dial error: timeout: context deadline exceeded"e
Please help as my all services are down. I tried everything but no luck.