1

App engine config:

instance_class: F1
automatic_scaling:
  min_idle_instances: automatic
  max_idle_instances: automatic
  min_pending_latency: automatic
  max_pending_latency: automatic

NodeJs application running: (i) Apollo server, (ii) Express, (iii) Knex

Steps:

  1. Get the Public IP Address of Cloud SQL Instance
  2. Get the Connection Name of Cloud SQL Instance
  3. Set the Connection Name of the Cloud SQL Instance in app.yaml
  4. Set the Public IP Address of the Cloud SQL Instance in index.ts of client

index.ts

// @note Using Knex
const db = await initDatabase({
    client: 'pg',
    connection: {
      host: args['blockchain-db-host'],
      port: args['blockchain-db-port'],
      database: args['blockchain-db-database'],
      user: args['blockchain-db-user'],
      password: args['blockchain-db-pass'],
    },
  })

N.B. Also get Username, Password, and set them in index.ts, also Port in index.ts used as 5432 as Cloud SQL Instance is a Postgres database.

app.yaml

beta_settings:
  cloud_sql_instances: <project>:<region>:<sql-instance>=tcp:5432

N.B. The beta_settings once deployed via gcloud app deploy is not reflected in config file on Google App Engine. Also, I am not familiar with the Unix socket methodology, I am assuming that 5432 should also be used as the PORT when setting up the App engine's cloud proxy connection.

2 Answers 2

2

I'd suggesting following the instructions in the documentation: Connecting to Cloud SQL from App Engine (Flex).

Specifically with your app.yaml, you need to connect with 172.17.0.1:PORT, where PORT is the port you specified (5432). This is because the Cloud SQL Auth proxy is listening on 172.17.0.1 to encrypt and forward connections to your Cloud SQL instance.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I had tried this but also ready somewhere that connecting using IP address over Unix socket connection strings required IP allow listing of the GAE instance's IP address to be allowed in the connection settings of the Cloud SQL instance. You can see my answer for what I ended up going with as a result of the findings mentioned here.
@matabeitt your answer looks incorrect as is because you have added =tcp:5432 in your app.yaml. If you want to use a unix socket you need to remove that argument.
1

Even though the Application is a flex environment, App Engine is allowed to connect to Cloud SQL via: (i) TCP, and (ii) Unix Socket

N.B. In my case, I would advise to use the Unix Socket in conjunction with Connection Instance, otherwise, use of IP Addresses typically require whitelisting and other configuration.

If you are using the Connection Instance Name provided by the Cloud SQL Instance, you should be using the Unix Socket methodology to establish the connection.

In this case, app.yaml must have:

beta_settings:
  cloud_sql_instances: <project>:<region>:<sql-instance>=tcp:5432

And index.ts must resemble:

const db = await initDatabase({
    client: 'pg',
    connection: {
      host: `/cloudsql/${args['blockchain-sql-connection-name']}`,
      port: args['blockchain-db-port'],
      database: args['blockchain-db-database'],
      user: args['blockchain-db-user'],
      password: args['blockchain-db-pass'],
    },
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.