Cloud Function times out connecting to Cloud SQL - All standard methods failed

Hello, I have a new project (financialnews-463800) and am unable to establish a database connection between a 2nd Generation Cloud Function (python312 runtime in asia-southeast1) and a Cloud SQL PostgreSQL instance in the same project and region. I have attempted and documented failures for both standard connection methods:> 1. Private IP Method: When deploying a function that uses the google-cloud-sql-connector[pg8000] library, the Cloud Build process fails with the error No matching distribution found.> 1. Public IP Method: When deploying a function that connects to the database’s Public IP (with the firewall authorized for 0.0.0.0/0), the function times out with a 504 Gateway Timeout. A diagnostic test proves the function can make other outbound HTTPS requests successfully, isolating the timeout to the database connection call.> Could you please advise on any potential networking policies or project settings that would prevent both of these standard connection methods from working?

Hi @eha2886 ,

Welcome to Google Cloud Community!

Private IP Method: The error “No matching distribution found” suggests that there might be issues with the dependencies or the environment setup.

  • Ensure that the “google-cloud-sql-connector” and “pg8000” libraries are properly specified in your “requirements.txt” file. Also, make sure that the versions specified are compatible with each other and the Python runtime you are using.
  • Ensure that you have set up Private IP for your Cloud SQL instance properly.

Public IP Method: The “504 Gateway Timeout” error indicates that your Cloud Function did not receive a timely response from the Cloud SQL PostgreSQL instance. While your diagnostic test confirms general outbound HTTPS requests work, the issue is most likely specific to the database connection.

  • Ensure your Cloud SQL instance is configured with a Public IP address.
  • Review Cloud Function Egress Settings: If the function is configured to “Route all traffic to the VPC” via a Serverless VPC Access connector, ensure a Cloud NAT gateway is properly configured in the associated VPC network. Without it, public internet-bound traffic will be dropped. If the function is using the default “Route only requests to private IPs to the VPC”, public IP traffic bypasses the VPC connector.

You can also visit this documentation that contains information and examples for connecting to a Cloud SQL instance from a service running in Cloud Run functions.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.