Connect GCP App Engine to Cloud SQL

This is another quick note for my own reference. For most steps I followed this documentation: Using Cloud SQL for MySQL, but there are some gotchas that are worth noting.

Before getting into the details, here are some background information for readers. My goal is to set up a node.js application on Google Cloud App Engine. I use a MySQL instance on Cloud SQL, and I access my DB via Sequelize.

Sequelize configuration

{
dialectOptions: {
// socketPath: '/cloudsql/{project-id}:{region}:{cloudsql-instance-name}'
socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`
}
}

Do not include host and port, or put host the same value as socketPath.

app.yaml

env_variables:
CLOUD_SQL_CONNECTION_NAME: {project-id}:{region}:{cloudsql-instance-name}
beta_settings:
cloud_sql_instances: {project-id}:{region}:{cloudsql-instance-name}

The beta_settings part can be easy to miss.

Permission

This part turns out unexpected, I didn’t figure it out until diving into error logs.

It seems both Cloud SQL API and Cloud SQL Admin API needs to be enabled. Both are not enabled by default. The documentation mentioned about Cloud SQL API, but I didn’t know Admin API is also required until actually try to deploy the app.

Fly me to the moon.