Search code examples
google-cloud-functionsgoogle-cloud-sqlfirebase-tools

Using firebase functions emulator with cloud SQL proxy


I deployed a firebase https function that calls a GCP MySQL instance and returns some data. How can I run this function in the firebase function emulator?

I loaded the function into the emulator along with a simple helloWorld https function. That returns nicely with the local URL (http://localhost:5001/INSTANCE/us-central1/helloWorld). The locally run SQL https function, however, returns {"errno":-4058,"code":"ENOENT","syscall":"connect","address":"/cloudsql/(deleted)","fatal":true} ... where I've deleted the instance connection name.

Next, I run gcloud_sql_proxy on my PC, and it claims to be listening on 127.0.0.1:3306. The https function continues to return the exact same error above, so I'm not sure if the emulated function is going through my gcloud_sql_proxy or still trying to connect to the cloud SQL directly - I am guessing the latter.

Should I also re-instrument the function itself to connect using host+port (127.0.0.1, 3306) versus the unix socketpath method that it's currently using? I was hoping the emulator would automagically do that so I can avoid going down that rabbit hole, as the cloud documentation only provided the socketpath method. But I do plan to try that next.

Thank you.


Solution

  • Based on the error message, I think the emulator is looking for a Unix socket and it sounds like you're starting the Cloud SQL Proxy with a TCP socket.

    Try using a Unix socket whose path matches your function. Something like:

    cloud_sql_proxy -dir /cloudsql
    

    This will create a Unix socket for all SQL instances in your active gcloud project.