Learning center

Private IP connections from Cloud Run to Cloud SQL time out

When Cloud Run times out on a Cloud SQL private IP, compare the revision's VPC egress with the last revision that connected, then test the route. A difference in network, subnet, connector or egress mode points at the deploy; identical settings that fail for both revisions point at the VPC: its firewall rules, the private services access peering, or the source range the instance accepts.

How the timeout shows up#

On this path the code opens a TCP connection to the instance's private IP on port 5432, with no Cloud SQL Auth Proxy in between. Connect with TCP PostgreSQL's documentation notes that a message such as Connection timed out can mean a lack of network connectivity or a firewall blocking the connection; nothing reached a server that could answer. Server start problems

A timeout at stage 1 looks like a timeout at stage 4 to the application, so first confirm the path and rule out the instance itself. Which path the revision uses · The four stages If gcloud sql operations list --instance INSTANCE shows a restart, failover or maintenance in the window, start there instead.

Check 1: compare the revisions' egress#

Cloud Run reaches a VPC network through Direct VPC egress or a Serverless VPC Access connector. The revision stores them as annotations: run.googleapis.com/network-interfaces holds the network, subnet and network tags for Direct VPC egress, run.googleapis.com/vpc-access-connector names a connector, and run.googleapis.com/vpc-access-egress sets all-traffic or private-ranges-only. Direct VPC egress · Revision template annotations

for r in PREVIOUS_REVISION NEW_REVISION; do
  gcloud run revisions describe "$r" \
    --project PROJECT --region REGION \
    --format='yaml(metadata.name, metadata.annotations)'
done

gcloud sql instances describe INSTANCE --project INSTANCE_PROJECT \
  --format='yaml(ipAddresses, settings.ipConfiguration.privateNetwork)'

privateNetwork is the VPC network from which the instance is reachable by private IP, and ipAddresses lists its PRIVATE address. Instance resource The egress modes differ in one way that matters here: private-ranges-only, the default, sends traffic to RFC 1918 and Private Google Access addresses through the VPC, and all-traffic sends everything. --vpc-egress

What differs between the revisionsReading
The new revision has no network-interfaces and no connectorThe deploy removed VPC access. The private IP has no route from this revision
Network or subnet differs, or the network is not the instance's privateNetworkThe revision sits in a network that does not reach the instance
The connector name differsThe new connector may sit in another network, or be unhealthy. Describe both connectors
Egress changed, and the instance's private IP is outside 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16Under private-ranges-only, traffic to that address may not enter the VPC. Compare with all-traffic
Nothing differsNot the revision's network settings. Go to check 2

Changing a revision's network or subnet takes a new deployment, so a difference here came from a deploy command, a YAML file or a Terraform template. IP address allocation

Check 2: test the route with Connectivity Tests#

Connectivity Tests simulates a packet's path through your VPC network: it checks the egress firewall rules and routes it can see, and for a Cloud SQL instance in Google's network it reports an overall reachability result for the last step. It supports Cloud SQL destinations that do not use Private Service Connect. Connectivity Tests overview

The following command creates a test resource in the project; it does not touch the service or the instance. For Direct VPC egress, use an address from the revision's subnet as the source, because Cloud Run assigns instance addresses from the subnet and they change. IP address allocation

gcloud network-management connectivity-tests create run-to-orders-db \
  --project PROJECT \
  --source-ip-address=ADDRESS_IN_SUBNET \
  --source-network=projects/PROJECT/global/networks/NETWORK \
  --destination-ip-address=INSTANCE_PRIVATE_IP \
  --destination-port=5432 --protocol=TCP

For a revision that uses a Serverless VPC Access connector, --source-cloud-run-revision takes the revision itself as the source. connectivity-tests create

Test resultReading
A firewall rule drops the packetRead that rule: who created or changed it, and whether it applies to the subnet or the revision's network tags
No route to the destinationThe peering or the route to the private services range is missing. List the network's peerings
ReachableThe VPC configuration allows the path. Go to check 3

To read the peering, gcloud compute networks peerings list --network NETWORK lists the network's peerings; the REST resource carries each peering's state and stateDetails, and Cloud SQL's troubleshooting guide asks for the peering to be ACTIVE. peerings list · Network resource · Cloud SQL troubleshooting

Check 3: does the instance accept the source range?#

Cloud Run's Direct VPC egress subnet can use RFC 1918 ranges, 100.64.0.0/10 or 240.0.0.0/4. Supported IPv4 ranges Cloud SQL authorizes private IP connections from RFC 1918 ranges automatically; a client in any other range must be listed in the instance's authorized networks, and the peering to Cloud SQL must export that subnet's routes, or connect through the Cloud SQL Auth Proxy. Connect from non-RFC 1918 addresses

The revision's subnet rangeReading
Inside RFC 1918Authorized automatically. The cause lies in checks 1 and 2
100.64.0.0/10 or 240.0.0.0/4, in authorizedNetworks, and the peering to Cloud SQL exports these routesAuthorized and routed. Look again at the firewall and the timing
100.64.0.0/10 or 240.0.0.0/4, in authorizedNetworks, but the peering does not export non-RFC 1918 routesThe instance has no route back to the revision. Export the subnet's routes on the peering
Outside RFC 1918 and not in authorizedNetworksThe instance does not accept this source. A move to a new subnet explains why the new revision fails and the old one did not

A worked fixture: a new network or a new rule#

The fixture is illustrative. The fictional service orders-api in europe-west1 connects to 10.40.0.3:5432, the private address of shop-prod:europe-west1:orders-db, whose privateNetwork is shop-vpc. orders-api-00018 takes all traffic at 09:12:30 UTC, and connections time out from 09:12:41.

Outcome A: the deploy moved the revision.

Revisionnetwork-interfacesEgressNew instance connects?
orders-api-00017network shop-vpc, subnet run-ew1private-ranges-onlyYes (09:20:02, tag prev)
orders-api-00018network default, subnet defaultprivate-ranges-onlyNo

orders-api-00018 was deployed from a YAML file copied from a staging service, and its network is default, not the instance's privateNetwork. The connectivity test from an address in the default subnet to 10.40.0.3:5432 finds no route to the destination. The deploy caused it. Route traffic back to orders-api-00017, then fix the network in the YAML file.

Outcome B: same settings, a new firewall rule. Both revisions use run-ew1, and a fresh instance of orders-api-00017 started through a tag at 09:16:40 times out too. The connectivity test from 10.8.0.10 to 10.40.0.3:5432 reports that the egress rule deny-egress-db-ranges drops the packet. The rule was created at 09:05 UTC. The deploy only coincided with it; a rollback will not help. Fix or scope the rule.

Where the checks mislead#

Connectivity Tests analyzes configuration. It does not track firewall connection state and cannot see inside Google's network for Cloud SQL, so a reachable result leaves the instance's own side unexamined. Connectivity Tests overview

New instances with Direct VPC egress can take a minute or more to establish connections at startup. A slow first connection after a scale-out may be this delay, not a broken route; a timeout shorter than a minute will report it as a failure. Direct VPC egress limitations

Direct VPC egress does not support VPC firewall rules logging, so the logs will not show which rule dropped a revision's packets. Use the connectivity test, or the firewall rule's change history. Direct VPC egress limitations For latency through a connector rather than a hard timeout, see Cloud SQL latency through a VPC connector.

Questions#

Why does Cloud Run time out connecting to the Cloud SQL private IP?

The packet does not reach the instance, or the instance does not accept its source. The revision may lack VPC egress or sit in another network, a firewall rule may drop the traffic, the peering may be missing, or the revision's subnet may be outside RFC 1918 without an authorized network.

Should Cloud Run use all-traffic or private-ranges-only egress to reach Cloud SQL?

Either works for an instance whose private IP is in an RFC 1918 range, since private-ranges-only sends those addresses through the VPC. all-traffic also routes public destinations through the VPC, which then need a path out, such as a Cloud NAT gateway. Static outbound IP

Does a private IP timeout mean the Cloud SQL instance is down?

Not by itself. Read the instance's operations for the window. If none overlaps and a fresh instance of the previous revision connects, the new revision's network settings are the likely cause.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading