Learning center

Direct TCP to a Cloud SQL public IP from Cloud Run fails or works only sometimes

Direct TCP to a Cloud SQL public IP needs the client's address in authorized networks, and by default Cloud Run has no fixed source address. If connections work from some instances and fail from others, the allowlist holds addresses from a shared pool. If they all fail after a deploy, the revision has probably stopped routing through the VPC and Cloud NAT that gave it a static address.

How the symptom shows up#

The code's host is the instance's public IP and its port is 5432: no /cloudsql/ socket, no connector. Cloud SQL requires the client's address or range in authorized networks when a client connects directly to the public IP. Authorized networks A client outside the list does not get a login prompt, so the failure sits at stage 1 of the four stages.

By default, Cloud Run workloads reach the internet from a regional pool of dynamic IP addresses shared across serverless workloads, and Google advises against relying on it when an external database uses an IP allowlist. A dedicated static address takes Direct VPC egress or a connector that sends all outbound traffic through the VPC, plus a Cloud NAT gateway with a reserved address. Static outbound IP

That gives two patterns:

  • Intermittent: someone added the addresses the service happened to use to the authorized networks. Instances that leave from those addresses connect; others do not.
  • All at once after a deploy: the service had a static NAT address, and the new revision lost the egress configuration that routed through it.

Check 1: does the revision route through Cloud NAT?#

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

Look for run.googleapis.com/network-interfaces or run.googleapis.com/vpc-access-connector, and run.googleapis.com/vpc-access-egress. Revision template annotations Only all-traffic sends a public destination through the VPC; private-ranges-only, the default, sends only RFC 1918 and Private Google Access addresses there. --vpc-egress

Previous revisionNew revisionReading
VPC egress with all-trafficNo VPC egress, or private-ranges-onlyThe deploy moved traffic to the instance off the NAT address and onto the shared pool. Route traffic back, then restore the egress setting
VPC egress with all-trafficSameThe route did not change. Check the NAT gateway's addresses against the allowlist
No VPC egressNo VPC egressThe service never had a fixed address. Go to check 2

Check 2: read the instance's allowlist and SSL mode#

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

ipv4Enabled says whether the instance has a public address, authorizedNetworks lists the allowed ranges, and sslMode sets how SSL/TLS is enforced. Instance resource

AllowlistReading
Holds individual addresses that no NAT gateway of yours ownsThey came from the shared pool. A new instance can leave from another address at any time; this explains intermittent failures
Holds the NAT gateway's reserved addressCorrect for a service that routes all traffic through that gateway
Was replaced recentlygcloud sql instances patch --authorized-networks replaces the whole list, so an edit for another client can drop yours. Read the instance's UPDATE operations

Configure authorized networks · Operation types

The SSL mode adds a second condition. ALLOW_UNENCRYPTED_AND_ENCRYPTED is the default; ENCRYPTED_ONLY accepts only SSL/TLS connections; TRUSTED_CLIENT_CERTIFICATE_REQUIRED also requires a valid client certificate. A direct connection must meet the mode itself, while connections through the Auth Proxy or the connectors are encrypted and verified automatically. Enforce SSL/TLS A connection that reaches the server and is refused for its SSL settings is a stage 3 problem with a different fix from an address that is not on the list.

Why the built-in connection avoids this#

The Cloud SQL Auth Proxy that Cloud Run embeds does not need the client's address in authorized networks: the instance needs a public IPv4 address, which does not have to be reachable from any external address. The proxy authorizes through IAM and the Cloud SQL Admin API and connects on port 3307. Auth Proxy requirements Cloud SQL lists the connectors as the option for clients with dynamically assigned or ephemeral addresses, which fits Cloud Run. Connection options

Moving to it is a deploy with --add-cloudsql-instances and a code change to the socket path, and it brings the identity requirements: Cloud SQL Client on the service account and the Admin API. Connect from Cloud Run · Which path the revision uses

A worked fixture: a shared pool and a lost NAT#

The fixture is illustrative. The fictional instance orders-db has the public address 203.0.113.20 and sslMode: ENCRYPTED_ONLY; the code of orders-api connects there with SSL.

Outcome A: intermittent. No revision of orders-api has VPC egress. authorizedNetworks holds 198.51.100.14/32 and 198.51.100.87/32, added in October under the name orders-api-egress. Since 06:00 UTC, about a third of new instances fail their first connection; the rest connect. Nothing was deployed. The two addresses were observed from the shared pool; new instances leave from addresses the list does not contain. Give the service a static address through Direct VPC egress, all-traffic and Cloud NAT, then replace the two entries with the NAT address. Or move to the built-in connection.

Outcome B: after a deploy.

Revisionnetwork-interfacesvpc-access-egressNew instance connects?
orders-api-00017shop-vpc / run-ew1all-trafficYes (09:20:02, tag prev)
orders-api-00018shop-vpc / run-ew1private-ranges-onlyNo (from 09:12:41)

authorizedNetworks holds 198.51.100.200/32, the NAT gateway's reserved address. orders-api-00018 was deployed at 09:12:30 UTC with --vpc-egress private-ranges-only, so its traffic to 203.0.113.20 no longer enters the VPC and leaves from the shared pool. Route traffic back to orders-api-00017 and restore all-traffic in the deploy.

Where the checks mislead#

An instance that connects once proves only that its current address is allowed. With the shared pool, a fresh instance of the previous revision may connect or fail by chance, so one start does not split deploy from environment here; the revision's egress settings do.

Direct VPC egress with Cloud NAT can add 30 seconds or more of startup delay; Google suggests connectors for better startup performance with Cloud NAT. A client with a short connect timeout can fail on that delay even when the address is allowed. Direct VPC egress limitations

The authorized networks list applies to direct connections. It plays no part when the same instance is also reached through the Auth Proxy, so another client that works does not show the list is correct for this one.

Questions#

Does Cloud Run have a fixed outbound IP address for Cloud SQL authorized networks?

Not by default. Cloud Run uses a shared regional pool of dynamic addresses. A static address requires Direct VPC egress or a connector with all-traffic egress and a Cloud NAT gateway with a reserved address.

Why does my Cloud Run service connect to Cloud SQL only sometimes?

On a direct public IP connection, the usual reason is an allowlist of addresses taken from the shared pool. Instances that leave from a listed address connect and the rest do not.

Do I need authorized networks when Cloud Run uses the Cloud SQL connection?

No. The built-in connection goes through the Cloud SQL Auth Proxy, which authorizes with IAM. No address needs to be added to authorized networks for it.

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