Setting up Cloud SQL connection through apphosting.yaml configuration

I’m trying to connect my Firebase App Hosting deployment to a Cloud SQL database automatically. The Google Cloud documentation explains how to set this up manually through the console when deploying Cloud Run services, but I need to configure this connection in my apphosting.yaml file for automatic deployments.

When I check the configuration of manually created deployments, I can see there’s an annotation that points to the Cloud SQL instance. However, the apphosting.yaml format doesn’t seem to support annotations in the same way.

I attempted to set this up using environment variables like this:

env:
  - variable: DATABASE_CONNECTION_STRING
    value: 'my-project:us-west1:my-database'

This approach didn’t work though. Does anyone know the correct syntax for establishing Cloud SQL connections in the apphosting configuration file? I can’t find proper documentation for the apphosting.yaml schema anywhere.

Yeah, the cloudSqlInstances config is the right way to go. I hit the same issue moving from manual console deployments to automated ones with apphosting.yaml. Firebase App Hosting automatically handles the Cloud SQL proxy connection when you add the instance to the cloudSqlInstances array - way different from Cloud Run where you’re stuck managing the proxy yourself. Just make sure your database connection code uses the Unix socket path /cloudsql/your-project:region:instance-name instead of TCP. App Hosting connects through the socket interface. This setup automatically handles all the service account permissions and proxy connections you’d normally have to configure manually in the console.

yea, I had that prob too last month. Just ditch the env vars and use the cloudSqlInstances field in your apphosting.yaml. Here’s what worked for me:

cloudSqlInstances:
  - my-project:us-west1:my-database

It fixed it right away, I was stuck on env vars for ages.

Hmm, that’s interesting. Are you mixing up the connection string format? Cloud SQL proxy needs a different format than just the instance name. What error messages do you see when it fails? Also, what db are you using - Postgres or MySQL?