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:
- 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 togcloud run deploy
- 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 thegcloud run deploy
command to make it happen.
- 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 theDATABASE_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 usesocket
instead ofhost
.