How to connect to Cloud SQL in Cloud Run using Prisma

This guide assumes that Cloud SQL is configured with a public IP (the default). There are essentially three steps you need to do when you want to connect to the Cloud SQL instance from Cloud Run using Prisma:

  1. Create an IAM service account with the Cloud SQL Client role and attach it to your Cloud Run service. When deploying from the command line, pass it viat the --service-account parameter to gcloud run deploy

  2. Make the Cloud SQL instance available to your Cloud Run service. That can be done in the web console. If you’re doing it in a deploy script, you’ll need the instance connection name. It’s typically a colon separated string like PROJECT_NAME:REGION:INSTANCE_NAME. Pass --add-cloudsql-instance INSTANCE_CONNECTION_NAME --update-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" to the gcloud run deploy command to make it happen.

  3. Finally, tell Prisma to use the socket made available by Cloud SQL Proxy in Cloud Run to connect to the database. To do so, add a host=/cloudsql/INSTANCE_CONNECTION_NAME URL param to the DATABASE_URL. The full URL will then look something like this:
    postgresql://username:password@localhost/db_name?host=/cloudsql/gcp_project:us-central1:db. If you’re using MySQL, you might want to use socket instead of host.

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s