Nov 18, 20265 min read

Cloud Run Direct VPC Egress vs Serverless VPC Access Connectors

Two ways for Cloud Run to reach private IPs such as Cloud SQL, Memorystore and internal services: how each works, what it costs, how many IP addresses it needs, and which to use with Cloud NAT.

Illustration for Cloud Run Direct VPC Egress vs Serverless VPC Access Connectors

Cloud Run runs outside your VPC by default, so a service that needs Cloud SQL on a private IP, a Memorystore instance, or an internal API behind an internal load balancer needs a path into the VPC. For years the only answer was a Serverless VPC Access connector, a small managed group of VMs that forwards traffic into the VPC.

Direct VPC egress, generally available since 2024, is the second option. It places Cloud Run instances on a subnet of your VPC directly, with no connector VMs to pay for or size. It is the better default for most services, with two exceptions to check before you switch: IP address consumption and startup time behind Cloud NAT.

Direct VPC egress vs connectors at a glance

Direct VPC egressServerless VPC Access connector
How it worksEach instance gets a subnet IPTraffic goes through connector VMs
Extra compute costNoneAt least 2 VM instances, always on
SubnetAt least /26, about 2 IPs per instanceA dedicated /28
ThroughputUp to 1 Gbps per instanceLimited by connector instance count and type
Firewall targetingNetwork tags on the serviceConnector's network tags
With Cloud NATStartup delays of 30 seconds or more possibleRecommended for faster startup
Use whenDefault for private accessStatic egress IP via NAT, or IP-constrained VPCs

Google keeps a comparison of the two alongside the setup guides.

Set up Direct VPC egress

Create or pick a subnet with room to grow. Cloud Run reserves addresses in blocks and uses roughly twice the number of running instances, and the Direct VPC docs require at least a /26. A service that can scale to 100 instances wants a /24.

~
$ gcloud compute networks subnets create run-egress \ --network=prod-vpc --region=us-central1 --range=10.20.0.0/24$ gcloud run deploy api --image=$IMAGE --region=us-central1 \ --network=prod-vpc --subnet=run-egress \ --network-tags=api \ --vpc-egress=private-ranges-only

private-ranges-only sends RFC 1918 and other private destinations through the VPC and everything else straight to the internet. all-traffic sends everything through the VPC, which you need for a NAT gateway or for egress firewall rules on internet traffic. The network tag lets you write firewall rules that allow only the api service to reach, say, port 5432 on the database.

Direct VPC egress has limits that matter in some setups: a quota caps how many instances per revision can use it, connections can take a minute or more to establish when an instance starts, and VPC Flow Logs do not show the revision name. Add a startup probe that opens a connection to your database, so instances only take traffic once the path works.

Set up a connector

A connector needs its own /28 subnet and runs between a minimum and maximum number of instances of a chosen machine type.

~
$ gcloud compute networks vpc-access connectors create run-connector \ --region=us-central1 --network=prod-vpc --range=10.8.0.0/28 \ --min-instances=2 --max-instances=3 --machine-type=e2-micro$ gcloud run deploy api --image=$IMAGE --region=us-central1 \ --vpc-connector=run-connector --vpc-egress=all-traffic

Connector instances are billed as Compute Engine VMs even when the service is idle, and a connector does not scale back down once it has scaled up, as the Serverless VPC Access docs note. Size the maximum for peak throughput, and replace the connector if it grows past what you need.

Static outbound IP with Cloud NAT

Partners that allowlist your IP need traffic to leave through a Cloud NAT gateway with a reserved address. Both methods support it with --vpc-egress=all-traffic. Google's Direct VPC page warns of cold start delays of 30 seconds or more when Direct VPC egress is used with Cloud NAT and recommends a connector for that case, so services that scale from zero and call an allowlisted API are the main reason to keep a connector today.

Best fit: Direct VPC egress for private access to Cloud SQL, Memorystore and internal services; a connector when you route all traffic through Cloud NAT and startup time matters.

How does Encore connect Cloud Run to private resources?

Encore

Encore brings infrastructure from code. When you declare a database or a cache and deploy to GCP, Encore creates a dedicated project and VPC for the environment, provisions Cloud SQL and Memorystore on private addresses, and connects the Cloud Run services to that VPC so they reach them without public IPs, as described in the GCP infrastructure docs. The project permissions Encore asks for include roles/vpcaccess.admin to manage Serverless VPC Access connectors, listed in the connect your cloud account docs.

package sessions import ( "time" "encore.dev/storage/cache" "encore.dev/storage/sqldb" ) // Cloud SQL and Memorystore on private IPs in the environment's VPC. var db = sqldb.NewDatabase("sessions", sqldb.DatabaseConfig{Migrations: "./migrations"}) var cluster = cache.NewCluster("sessions", cache.ClusterConfig{ EvictionPolicy: cache.AllKeysLRU, }) type Session struct { UserID string `json:"user_id"` } var Active = cache.NewStructKeyspace[string, Session](cluster, cache.KeyspaceConfig{ KeyPattern: "session/:key", DefaultExpiry: cache.ExpireIn(30 * time.Minute), })

For a static outbound IP, the static IP guide uses the same pieces as above: create a Cloud NAT gateway with a reserved IP in the Encore-provisioned VPC, then set VPC egress to All traffic on the environment's Infrastructure page in the Encore Platform dashboard and deploy. Encore leaves the NAT gateway and reserved IP you created untouched on later deploys.

An agent adding a cache or a second database to a service writes the declaration in Go and tests it against local infrastructure; the private networking that connects it in production is the same for every resource, so there is no connector or subnet to add in the pull request.

Best fit: teams on GCP who want Cloud Run services on private networking to Cloud SQL and Memorystore from the first deploy, with a documented path to a static egress IP.

How to choose

Use Direct VPC egress for new Cloud Run services that need private IPs, with a /24 or larger subnet and a startup probe. Keep or add a connector when all traffic goes through Cloud NAT and cold starts matter, or when your VPC cannot spare the addresses. On Encore, private connectivity to declared databases and caches is set up per environment, and the egress setting is a dashboard option.

Frequently asked questions

What is Cloud Run Direct VPC egress?

Direct VPC egress gives each Cloud Run instance an IP address in a subnet of your VPC, so traffic to private IPs goes straight onto the network with no connector in between. You choose the network and subnet on the service and set vpc-egress to private-ranges-only or all-traffic.

Is Direct VPC egress cheaper than a VPC connector?

Usually. A Serverless VPC Access connector runs at least two VM instances that you pay for around the clock, while Direct VPC egress has no instances of its own and you pay only standard network charges. The trade-off is IP addresses: Direct VPC needs a subnet of at least /26 and uses about two addresses per running instance.

Should I use Direct VPC egress with Cloud NAT for a static outbound IP?

Yes. It works, but Google's documentation notes cold start delays of 30 seconds or more on instance startup when Direct VPC egress is combined with Cloud NAT, and recommends Serverless VPC Access connectors with Cloud NAT for better startup performance. Set egress to all-traffic in either case.

How does Encore connect Cloud Run services to private resources?

Encore provisions a VPC per environment and connects Cloud Run services to it so they reach Cloud SQL and Memorystore on private addresses. The VPC egress setting is exposed in the Encore Platform dashboard, where switching it to All traffic plus a Cloud NAT gateway gives services a static outbound IP.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders