1

I'm currently really frustating cause cannot deploy my app to Heroku and connect my db to Google Cloud SQL.

What am I already did?

  • I have been set up SSL to connect them
  • I have been set up Cloud SQL Auth Proxy (+ set up Cloud SQL CLient role in IAM Manager)
  • I have been did buildpacks via Heroku approaches etc.

And overall failed.

This is my config database via Typeorm in Nestjs.

    import { TypeOrmModuleOptions } from "@nestjs/typeorm"
    const fs = require("fs");
    require('dotenv').config()

    export class OrmConfig {
        // Config TypeOrm
        getConfig() {
             const ormConfig: TypeOrmModuleOptions = {
                 type: 'postgres',
                 url: 'postgres://myusername:mypassword@myhostincloudsql(publicip):5432/mydb',
                 // host: 'myhostincloudsql(public ip)',
                 // port: 5432,
                 // username: 'myusername',
                 // password: 'mypassword',
                 // database: 'mydb',
                 entities: ['dist/**/*.entity{.js,.ts}'],
                 synchronize: false,
                 migrationsRun: true,
                 logging: false,
                 logger: 'file',
                 migrations: [
                     'dist/database/migrations/**/*{.js, .ts}'
                 ],
                 cli: {
                     migrationsDir: 'database/migrations'
                 },
                 ssl: true,
                 extra: {
                      ssl: {
                           ca: fs.readFileSync('./src/config/server-ca.pem'),
                           cert: fs.readFileSync('./src/config/client-cert.pem'),
                           key: fs.readFileSync('./src/config/client-key.pem'),
                           // rejectUnauthorized: false
                      }
                 }
            }

            return ormConfig
        }
    }

    export default new OrmConfig().getConfig()

This is my app.module.ts

    import { SendGridModule } from '@anchan828/nest-sendgrid';
    import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
    import { ConfigModule } from '@nestjs/config';
    import { TypeOrmModule } from '@nestjs/typeorm';
    // import { BlogModule } from './blog/blog.module';
    import OrmConfig from './config/ormConfig';
    import { BusinessContactModule } from './contact/business/business.module';
    import { InvestorNotificationController } from './contact/investor/investment-recommendation/investor-notification.controller';
    import { InvestmentRecommendationModule } from './contact/investor/investment-recommendation/investor-notification.module';
    import { InvestorContactModule } from './contact/investor/angel-investor/investor.module';
    import { EmitenCampaignModule } from './emiten-campaign/emiten-campaign.module';
    import { EmitenModule } from './emiten/emiten.module';
    import { FlashMobileModule } from './flash-mobile/flash-mobile.module';
    import { CoreModule } from './global/core.module';
    import { Agent } from './middleware/agent';
    import { AuthModule } from './middleware/auth.module';
    import { TekenajaModule } from './tekenaja/tekenaja.module';
    import { UserModule } from './user/user.module';
    require('dotenv').config();

    @Module({
       imports: [
         /**
         * ================================
         * Typeorm Config
         * ================================
         */
         SendGridModule.forRoot({
           apikey: process.env.SENDGRID_API_KEY,
         }),
         TypeOrmModule.forRoot(OrmConfig), // This is I call it
         ConfigModule.forRoot(),
         AuthModule,
         TekenajaModule,
         FlashMobileModule,
         CoreModule,
         EmitenModule,
         EmitenCampaignModule,
         // InvestorContactModule,
         // BusinessContactModule,
         UserModule,
         // BlogModule
         InvestmentRecommendationModule
       ]
     })
     export class AppModule implements NestModule { 
       configure(consumer: MiddlewareConsumer) {
         consumer
         .apply(Agent)
         .forRoutes('/');
       }
     }

NB: Anyway, I'm using public ip in Google Cloud SQL

I have tried connect it from Heroku Postgres, and works. And I try with Cloud SQL fields, getting error something like this

Error response

Now, what should I do in order to connected between them? I hope reply my issue guys. Many thanks.

Jofre
  • 3,718
  • 1
  • 23
  • 31
  • [Please do not post images of code, data, error messages, etc.](https://stackoverflow.com/help/how-to-ask#:~:text=DO%20NOT%20post%20images%20of%20code%2C%20data%2C%20error%20messages%2C%20etc.) – Jofre Jan 13 '22 at 14:50

3 Answers3

0

Try running the Cloud SQL Proxy in addition to your app in the same dynamo.

For example:

web: trap '' SIGTERM; ./cloud_sql_proxy -credential_file sa.json -instances=INSTANCE_CONN_NAME=tcp:5432 & npm start & wait -n; kill -SIGTERM -$$; wait

Note, you'll need to ensure the Cloud SQL Proxy binary and a credential file is included with your app. And you won't want to check those into source control.

See https://help.heroku.com/CTFS2TJK/how-do-i-run-multiple-processes-on-a-dyno.

enocom
  • 1,496
  • 1
  • 15
  • 22
0

In CloudSQL - your instance must have Public IP address ( section Connection ) and you need address IP in Authorised network from where you will try connect. Ofc you can setup 0.0.0.0 but don't do it. Better way is setup ssh connection.

Public Adress

  • I have been did it, but still didn't work. The result in heroku logs still same like image above. Did you know how to get the ip from heroku app? I'm currently using quotaguard to get static ip, but still no effect – Pandhu Wibowo Jan 14 '22 at 04:02
  • Heroku use dynamic IP. you need set 0.0.0.0 in autorised network. I checked with Heroku and its works. – Sebastian Pietrzak Jan 14 '22 at 22:15
  • I already set the ip. Still getting error 2022-01-15T06:26:43.855533+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 120 seconds of launch 2022-01-15T06:26:43.928593+00:00 heroku[web.1]: Stopping process with SIGKILL 2022-01-15T06:26:44.018963+00:00 app[web.1]: Error waiting for process to terminate: No child processes 2022-01-15T06:26:44.251980+00:00 heroku[web.1]: Process exited with status 22 2022-01-15T06:26:44.341858+00:00 heroku[web.1]: State changed from starting to crashed – Pandhu Wibowo Jan 15 '22 at 06:32
0

I have solved my issue. As mentioned @Sebastian Pietrzak, I should implemented 0.0.0.0/0 in Authorized Network in the Connection Menu. And for the additional you have to set the SSL appoach.

enter image description here

Many thanks everyones