Chapters

Serverless networking: Cloud Run

Everything so far was about VMs, which sit inside the VPC. Cloud Run does not. This chapter covers how a Cloud Run service reaches private things and the internet through your VPC, and how to control who can reach the service itself. The two directions are set separately, and mixing them up is the most common Cloud Run networking mistake.

Leaving and reaching privately · chapter 13 of 26 · 10 min read

Cloud Run lives outside your VPC

A VM has a network card in your subnet. It has an internal IP, it is part of the VPC, and chapters 1.1 to 7 apply to it directly. Cloud Run has none of that. It runs containers that Google starts and stops on demand, on Google's own infrastructure, outside your VPC.

By default, an outgoing call from Cloud Run goes straight to the internet from Google's serverless infrastructure. It never touches your VPC, routes, firewall or Cloud NAT. Two problems follow:

Google's serverless networkCloud Run kade-apino network cardno internal IPPayGateallows only 34.87.200.7✕source: a random Google IPnot on the allow-listkade-vpckade-db10.10.2.5✕no path in
  • Run by Google
  • Your VPC

Wide by design - scroll it sideways.

#ProblemWhy
1It cannot reach private thingskade-db's 10.10.2.5 exists only inside kade-vpc, and Cloud Run is not in it
2Its outgoing IP is unpredictableTraffic leaves from a shared pool of Google addresses that changes all the time. There is nothing to put on PayGate's allow-list

A real backend, one that talks to a private database and needs a fixed outgoing IP, must be connected to the VPC.

Two ways to connect to the VPC

kade-vpckade-db10.10.2.5A · connectorkade-apiCloud Runconnector · 2 to 10 VMsown /28 · billed all the timeextra hopB · Direct VPC egresskade-apiCloud Runsn-run 10.10.3.0/24the instance's own address10.10.3.7 · no extra costdirect
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

Wide by design - scroll it sideways.

#Serverless VPC Access connectorDirect VPC egress
1How it worksA group of managed VMs that forward traffic from Cloud Run into your VPCEach Cloud Run instance gets its own internal IP in a subnet you choose
2Addresses usedA dedicated /28 for the connector VMsAbout 2 per instance, from your subnet (below)
3CostThe connector VMs are billed all the time, used or notNo extra charge; network traffic only
4ThroughputLimited by how many connector VMs you allowGrows with the service
5LatencyAn extra hopLower
6Something to runYes, the connectorNo
7StatusThe original methodWhat Google now recommends
The connector's traffic uses the special path 35.199.224.0/19 from chapter 4. Once traffic is inside the VPC by either method, it follows your routes and your firewall rules like any other VPC traffic.

Sizing the subnet for Direct VPC egress

This is the one planning step. Cloud Run takes addresses from the subnet in blocks of 16 (/28) so it can scale quickly, uses about 2 addresses per instance in steady state, and keeps a scaled-down revision's addresses for up to 20 minutes. During a deploy, the old and new revisions can both hold addresses:

Kadé: kade-api max 50 instances

During a deploy:  (50 old + 50 new) × 2 = 200 addresses
sn-run 10.10.3.0/24 → 252 usable (chapter 2)  → fits, with a small margin

A /26 (60 usable) would run out during the first deploy under load.
sn-run as a /2416 blocks of 16 addressesold revision · 50 × 2 = 100new revision · 100 morefreesn-run as a /264 blocks✕full at about 30 instancesa deploy at 50 needs 200 addresses
  • Addresses held in sn-run (dashed: the new revision)

Wide by design - scroll it sideways.

The addresses change all the time, so never write firewall rules for single Cloud Run IPs. Use the whole subnet range, the service's network tags, or its service account.

Egress: which traffic goes through the VPC

There are two layers of setting. First, is the service connected to the VPC at all? Second, if it is, which of its traffic uses that connection: private-ranges-only or all-traffic?

The drawing follows the same two calls, one to the database and one to PayGate, under each setting:

1 · not connectedkade-apiCloud Runkade-vpckade-db 10.10.2.5NAT 34.87.200.7PayGatesees a random IP✕straight to the internet, never through kade-vpc2 · connected, private-ranges-onlykade-apiCloud Runkade-vpckade-db 10.10.2.5NAT 34.87.200.7PayGatesees a random IPstraight to the internet, never through kade-vpc3 · connected, all-traffickade-apiCloud Runkade-vpckade-db 10.10.2.5NAT 34.87.200.7PayGatesees 34.87.200.7
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

Wide by design - scroll it sideways.

#SettingThrough the VPCFits
1Not connectedNothingA service with nothing private to reach
2private-ranges-onlyOnly traffic to private ranges (RFC 1918, plus the Private Google Access addresses)A private database, and nothing that checks the caller's IP
3all-trafficEverything, so internet calls leave through Cloud NATAnything that allow-lists your outgoing address, like PayGate

Two things to add when you choose all-traffic:

#NeedWhy
1Cloud NAT must cover sn-runOtherwise internet calls enter the VPC and find no way out. Add sn-run to kade-nat (chapter 7 section 03)
2Google APIs still workCalls to Google APIs now go through the VPC too. Private Google Access handles them, and Cloud NAT turns it on for sn-run automatically (chapter 7 section 02)

Firewall rules for Cloud Run traffic

Once kade-api's traffic enters kade-vpc from sn-run, kade-db's ingress rules judge it like any other packet (chapter 5.1). The rule kade-api-to-db allowed traffic from VMs running as sa-kade-api. Cloud Run is not a VM, so give it a rule of its own:

Ingress: who may reach the service

Ingress is a completely separate setting. It controls who may reach the service, whatever the egress settings are.

#Ingress valueReachable fromBlocked
1allAnyone on the internet, directly at the run.app addressNothing
2internalYour VPC, plus a few Google services such as Pub/Sub and EventarcThe run.app address from the internet
3internal-and-cloud-load-balancingYour VPC, plus a load balancer in frontDirect internet access to run.app
"Your VPC" here means requests that arrive from inside it, for example from a VM without an external IP, which reaches Cloud Run through Private Google Access.
Shopperapi.kade.lkCloudflarechapter 13load balancer+ Cloud Armorkade-apiCloud RunAttackeranywhereingress: allkade-api-xxxx.run.appskips Cloudflare, the load balancer and Cloud Armor
  • Run by Google
  • Allowed traffic and replies
  • Firewall and access rules

Wide by design - scroll it sideways.

The ingress setting is one of three checks a request has to pass, in this order:

a requestany callerDefault URLrun.app exists?--no-default-urlno → 404Ingresspath allowed?--ingressno → 404IAM invokercaller allowed?roles/run.invokerno → 403kade-apiruns the request
  • Run by Google
  • Allowed traffic and replies
  • Firewall and access rules

Wide by design - scroll it sideways.

#LayerQuestion it answersSet with
1Ingress settingCan this network path reach the service at all?--ingress
2IAM invoker checkIs this caller allowed to invoke it?roles/run.invoker, or --allow-unauthenticated for public services
3Default URLDoes the run.app address exist at all?--no-default-url removes it
A public shop API behind a load balancer must allow unauthenticated calls, because shoppers have no Google identity. That makes the ingress setting the thing that protects it, which is why getting it right matters so much.

One service calling another

Kadé has one service today. Suppose a second one appears: kade-notify, which sends order emails and should never be reachable from the internet, so its ingress is internal. kade-api now has to call it, and two things must both be true:

kade-apithe callerkade-notifyingress: internal✕not through the VPC: arrives as internet traffic404kade-vpcsn-runegress all-trafficPrivateGoogle Accessthen: IAM checktoken + run.invoker
  • Run by Google
  • Your VPC
  • Allowed traffic and replies
  • Firewall and access rules

Wide by design - scroll it sideways.

#CheckWhat makes it pass
1The call arrives as internalIt must travel through the VPC. all-traffic egress does this, with Private Google Access on the subnet. A call from a service that is not connected, or uses private-ranges-only with no extra DNS setup, goes around the VPC and gets a 404
2The caller is allowedkade-api's service account has roles/run.invoker on kade-notify, and each request carries an identity token for it. No --allow-unauthenticated on an internal service

Scaling multiplies everything

A VM is one machine with one address. A Cloud Run service is as many copies as the load needs, and each copy brings its own connections, ports and addresses:

kade-apiup to 50 instanceseach brings its own× 50Database connections50 × a pool of 10 = 500keep under the database's limitNAT ports50 instances calling PayGatereuse connections (chapter 7)sn-run addresses50 × 2 = 100, 200 in a deploysize the subnet first
  • Run by Google
  • Your VPC

Wide by design - scroll it sideways.

#GuardWhy
1Set --max-instancesIt caps all three at once. Kadé uses 50
2Keep the pool per instance smallMax instances × pool size must stay under the database's connection limit, or a traffic spike locks everyone out of the database
3Reuse outgoing connectionsEvery instance draws on kade-nat's ports when it calls PayGate (chapter 7 section 05)

What else is different from a VM

#TopicOn Cloud Run
1Its addresses in sn-runFor outgoing traffic only. Nothing can connect to an instance's 10.10.3.x address; requests always come in through Google's front end
2A fixed address to come in onThere is none. The load balancer provides it (chapter 11)
3HTTPSGoogle's front end handles TLS and passes plain HTTP to the container's port (8080 by default)
4Private DNSOnce connected to kade-vpc, the service resolves the VPC's private zones, so db.kade.internal works (chapter 8)
5Limiting what it may callIts traffic in the VPC obeys egress firewall rules too. Give the service a network tag and write rules for it (chapter 5.1)
6Jobs and functionsCloud Run jobs and Cloud Run functions take the same VPC and egress settings

Quick reference

Read any Cloud Run service's networking as two separate questions:

#DirectionQuestionOptions
1IngressThe door in: who can reach it?all · internal · internal-and-cloud-load-balancing
2EgressThe door out: what can it reach, and from which IP?Not connected, or connected with private-ranges-only or all-traffic
#IngressEgressReaches private IPsInternet egress IPFits
1allnone✗Random Google poolPublic API with nothing private, and no load balancer protection
2allprivate-ranges-only✓Random Google poolPublic service with a private database; nothing allow-lists it
3internal-and-cloud-load-balancingall-traffic✓Fixed NAT IPThe hardened production shape: behind a load balancer and WAF, private database, allow-listable egress. Kadé's choice
4internalall-traffic✓Fixed NAT IPAn internal-only service calling an allow-listed partner API

Try it: what can reach kade-api, and what can it reach?

Ingress
Egress
Cloud NAT covers sn-run

3 of 6 paths are not what Kadé needs.

PathWhat happens
ingressShopper through the load balancerReaches kade-api through the load balancer and Cloud Armor
ingressAnyone at the run.app addressReaches kade-api directly, skipping the load balancer and Cloud Armor (the trap)
ingresskade-worker, from inside kade-vpcReaches kade-api: counts as internal under every setting
egresskade-api → kade-db 10.10.2.5:5432Fails: kade-api is not in the VPC, and 10.10.2.5 exists only there
egresskade-api → PayGate (allows only 34.87.200.7)Leaves from a random Google IP; PayGate refuses it
egresskade-api → Cloud Storage APIDirectly from Google's serverless network

Back to Kadé

Commands in this chapter

Commands 1 and 2 are read-only. Commands 3 to 5 change the project.

See a service's ingress and egress settings

  • What it shows: The ingress value and the VPC settings of kade-api's current revision.
  • What to look for: The run.googleapis.com/ingress annotation, and run.googleapis.com/network-interfaces and vpc-access-egress in the revision template.
gcloud run services describe kade-api --region=asia-southeast1 --format=yaml \  | grep -E "ingress|network-interfaces|vpc-access-egress"

Find every service with ingress "all"

  • What it shows: Each Cloud Run service in the region with its ingress setting.
  • What to look for: all on anything that has a load balancer in front of it (the trap in section 05).
gcloud run services list --region=asia-southeast1 \  --format="table(metadata.name,metadata.annotations['run.googleapis.com/ingress'])"

Create the Cloud Run subnet and add it to the NAT (changes the project)

  • What it shows: Step 1: sn-run exists and Cloud NAT covers it.
  • What to look for: The NAT's subnet list now includes sn-run. The --nat-custom-subnet-ip-ranges flag replaces the list, so name all three.
gcloud compute networks subnets create sn-run \  --network=kade-vpc --region=asia-southeast1 --range=10.10.3.0/24gcloud compute routers nats update kade-nat \  --router=kade-router --region=asia-southeast1 \  --nat-custom-subnet-ip-ranges=sn-app,sn-data,sn-run

Deploy with Direct VPC egress and locked ingress (changes the project)

  • What it shows: Step 3: all outgoing traffic through kade-vpc, reachable only from the VPC and the load balancer.
  • What to look for: Command 1 shows both settings. Replace the image path with your own.
gcloud run deploy kade-api --region=asia-southeast1 \  --image=asia-southeast1-docker.pkg.dev/kade-prod/kade/api:latest \  --network=kade-vpc --subnet=sn-run --vpc-egress=all-traffic \  --ingress=internal-and-cloud-load-balancing \  --allow-unauthenticated --max-instances=50

Close the run.app bypass on an existing service (changes the project)

  • What it shows: The one-line fix for the trap in section 05.
  • What to look for: The run.app address returns 404 from outside, while the site still works through the load balancer. Set up the load balancer first, or the service becomes unreachable.
gcloud run services update kade-api --region=asia-southeast1 \  --ingress=internal-and-cloud-load-balancing

Spotted a mistake, or something unclear? Suggest an edit on GitHub

See it broken

Harbour is a platform built wrong on purpose. These defects are this chapter's ideas, left undone: what each one breaks, how you would find it, and the change that closes it.

  1. D6
    Cloud Run ingress is left at all

    criticalcloses in phase 3, Close the bypasses

    Goes with Ingress: who may reach the service

  2. D7
    Cloud Run egress is private-ranges-only

    highcloses in phase 5, Egress and load balancer

    Goes with Egress: which traffic goes through the VPC