0
votes

I am trying to connect a Google Compute Engine instance with a MySQL database to Google App Engine using Laravel. I actually have connected my Google App Engine to a Cloud SQL instance, I don't have problem with this, but I need an additional database connection with the database located on Google Compute Engine.

Google Compute Engine instance is on a different project. This is my scheme:

Project A -> Compute Engine -> Instance -> MySQL database

Project B -> App Engine -> Laravel

Project B -> Cloud SQL -> DB-instance -> MySQL database

This is my app.yaml file:

runtime: php
env: flex

runtime_config:
  document_root: public

# Ensure we skip ".env", which is only for local development
skip_files:
  - .env

env_variables:
  # Put production environment variables here.
  APP_LOG: "errorlog"
  APP_KEY: "[KEY]"
  STORAGE_DIR: "/tmp"
  CACHE_DRIVER: "database"
  SESSION_DRIVER: "database"
  APP_DEBUG: "true"

  #CLOUD SQL database connection
  DB_CONNECTION: "[DATABASE_1_NAME]"
  DB_HOST: "[CLOUD_SQL_INSTANCE_NAME]"
  DB_DATABASE: "[DATABASE_1_NAME]"
  DB_USERNAME: "root"
  DB_PASSWORD: "[PASSWORD]"
  DB_SOCKET: "/cloudsql/[PROJECTB]:[REGION]:[CLOUD_SQL_INSTANCE_NAME]"

  # COMPUTE ENGINE database connection
  DB_HOST_2: "[COMPUTE_ENGINE_INSTANCE_NAME]"
  DB_DATABASE_2: "[DATABASE_2_NAME]"
  DB_USERNAME_2: "root"
  DB_PASSWORD_2: "[PASSWORD]"

beta_settings:
    cloud_sql_instances: "[PROJECTB]:[REGION]:[CLOUD_SQL_INSTANCE_NAME]"
1
Sorry, but it's unclear what your question is. - Vadim
My question is, how can I connect a database hosted on Compute Engine to a Laravel application hosted on App Engine? - Diego Blaker
The biggest challenge would probably be managing firewall rules to allow connections from App Engine since you would not know what he IP would be. One way to work around this is to configure your database server to only allow SSL cert-based authentication and allow connections from anywhere. - Vadim

1 Answers

0
votes

I consider you're using Google App Engine Standard environment with PHP. When you connect a GAE Application to a DB hosted on a Google Compute Engine instance, it is treated as a connection to an "external DB". Thus, you need to consider GAE as an external client of your GCE instance.

Therefore, you must configure your VPC firewall in order to accept connections to the port 3306 from outside and you must specify the external GCE instance IP address as the host of your connection string. Be sure that your firewall rule accepts connections from everywhere (IP mask: 0.0.0.0/0).

Hope this helps you. Bye