Your vendor wants to allowlist one IP. Cloud Run doesn’t have one. Here’s the Cloud NAT setup that fixes it, what it costs, and the three ways it bites you.
The vendor’s onboarding form had one field I couldn’t fill in: “Your egress IP address.”
My service runs on Cloud Run. Cloud Run doesn’t have an egress IP address — not a fixed one, anyway. Every request leaves from an address out of a large pool Google manages, and the address changes. There is no setting in the Cloud Run console that says “use this IP.” The form might as well have asked for the colour of my load balancer.
This is a common shape of problem, and it comes up more than you’d expect: a third-party API, a partner’s firewall, a legacy system behind a corporate VPN, all gated on “we’ll allowlist your IP.” IP allowlisting is a weak form of authentication and I’d rather the vendor offered token auth or mutual TLS — but sometimes you don’t get to choose the vendor’s security model. You just need one stable IP.
This article is the setup that gives Cloud Run a single static egress IP: Direct VPC egress into a VPC, a Cloud Router, and a Cloud NAT gateway with a manually reserved external address. I’ll walk through the architecture, the exact commands, the cost, and the failure modes — including the one that silently routes your Google API traffic through the NAT gateway and bills you for it.
The mental model: Cloud Run borrows your VPC’s identity
Cloud Run itself never gets a static IP. Instead, you route its outbound traffic through your own VPC, and the VPC’s NAT gateway stamps every outbound packet with one reserved address. From the vendor’s point of view, your requests come from that one IP. From Google’s point of view, your service is just another workload in your VPC.
The pieces:
- A VPC network with a subnet in your Cloud Run region.
- Direct VPC egress on the Cloud Run service, which routes the service’s outbound traffic into that VPC. This is the newer path — it replaced the serverless VPC access connector, and there’s no connector to manage or pay for.
- A Cloud Router in the same region, which is the control plane Cloud NAT needs.
- A Cloud NAT gateway attached to that router, configured with a manually reserved static external IP address instead of an automatically assigned one.
- Private Google Access on the subnet, so traffic to Google APIs stays off the NAT gateway (more on why below).
One important caveat up front: this is a per-region setup. A Cloud NAT gateway lives in one region, and a Cloud Run service in another region can’t use it. If you run in two regions, you build this twice — two NAT gateways, two static IPs — and you give the vendor both addresses.
Step 1: Reserve the static IP
Reserve the address first, before anything references it, so you know exactly which IP the vendor will see:
gcloud compute addresses create scoring-egress-ip \
--region us-east1
gcloud compute addresses describe scoring-egress-ip \
--region us-east1 \
--format="value(address)"
Write that address down and send it to the vendor. Everything after this step is making sure that address is the only one your traffic ever leaves from.
Step 2: VPC, subnet, router, NAT gateway
If you already have a VPC with a subnet in your region, skip to the router. Otherwise create them:
gcloud compute networks create scoring-vpc \
--subnet-mode=custom
gcloud compute networks subnets create scoring-subnet \
--network=scoring-vpc \
--region us-east1 \
--range=10.10.0.0/24 \
--enable-private-ip-google-access
That — enable-private-ip-google-access flag matters more than it looks. With it, traffic from the subnet to Google APIs (Cloud Storage, Secret Manager, BigQuery, the Container Registry) goes over Google’s private network instead of through the NAT gateway. Without it, every call your service makes to a Google API gets NAT’d, and you pay NAT data-processing charges on traffic that should have been free. I only learned this when I noticed a NAT bill line that didn’t match my external API volume.
Now the router and the NAT gateway, pointed at the reserved address:
gcloud compute routers create scoring-router \
--network=scoring-vpc \
--region us-east1
gcloud compute routers nats create scoring-nat \
--router=scoring-router \
--region us-east1 \
--nat-custom-subnet-ip-ranges=scoring-subnet \
--nat-external-ip-pool=scoring-egress-ip
Two choices here are deliberate. — nat-custom-subnet-ip-ranges limits NAT to just the subnet Cloud Run uses, so nothing else in the VPC accidentally starts egressing through your reserved IP. And — nat-external-ip-pool with the address you reserved is what makes the egress IP static — if you leave this off, Cloud NAT auto-assigns addresses from a pool and your “static” IP stops being static.
Step 3: Point Cloud Run at the VPC
The Cloud Run side is one annotation on the service — Direct VPC egress, all-traffic mode, so every outbound request goes through the VPC and out the NAT gateway:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: scoring-api
spec:
template:
metadata:
annotations:
run.googleapis.com/vpc-access-egress: all-traffic
spec:
containers:
- image: us-east1-docker.pkg.dev/PROJECT/REPO/scoring-api@sha256:DIGEST
Or with gcloud:
gcloud run services update scoring-api \
--region=us-east1 \
--vpc-egress=all-traffic \
--network=scoring-vpc \
--subnet=scoring-subnet
If your service only talks to the vendor and to Google APIs, you could use private-ranges-only egress — but then the vendor traffic, which goes to a public IP, wouldn’t route through the VPC at all, and you’d be back to ephemeral Cloud Run IPs. For this setup, all-traffic is the point.
Step 4: Prove it — don’t trust the config
The config can look right and still be wrong. The NAT gateway might be attached to the wrong subnet, the service might be in a different region than the router, or the vendor might have allowlisted a different address than the one you reserved. Verify from inside the running service, not from your laptop:
import requests
@app.get("/debug/egress-ip")
def egress_ip():
# Only enable this behind auth, and remove it when you're done.
resp = requests.get("https://ifconfig.me", timeout=10)
return {"egress_ip": resp.text.strip()}
Hit that endpoint, confirm the address matches the reserved one, then delete the endpoint. A debug endpoint that tells the world your egress IP is a small information leak you don’t need to keep — and more importantly, any unauthenticated endpoint that makes outbound requests on demand is a request-forgery foothold.
A better permanent check is a log-based one: have the service log the response of a periodic health check against the vendor’s API, and alert if the vendor starts rejecting requests. If the NAT gateway ever gets recreated with a different address, the vendor’s 403s are your first signal.
What it costs
A NAT gateway bills in two parts: a fixed hourly charge for the gateway itself, and a per-GB charge for data processed through it. The hourly charge is small in absolute terms but it’s a fixed cost — it runs whether your service handles one request a day or a million, and for a solo builder it can easily be larger than the Cloud Run bill it sits in front of. Check the current Cloud NAT pricing page before you commit; the point is structural, not the exact number: you’re adding a 24/7 fixed-cost component to a service you chose partly because it scales to zero.
The per-GB charge is where Private Google Access pays for itself. Without it, every byte your service sends to a Google API — every Secret Manager read, every Storage upload, every BigQuery query — is NAT-processed data. With it, that traffic never touches the gateway. Set it on the subnet and verify it took effect; it’s a subnet-level setting and it’s easy to assume it’s on when it isn’t.
The three ways this bites you
- SNAT port exhaustion. A single NAT IP address has a limited pool of source ports — roughly 64,000 per destination. Cloud NAT spreads ports across instances automatically, but a service making many concurrent outbound connections to the same destination can run out. The symptom is maddening: intermittent connection timeouts that look like the vendor is flaky, with no errors in your service logs except failed requests. If you see it, the fixes are, in order: reduce concurrent connections per instance (lower Cloud Run concurrency), add a second reserved IP to the NAT pool, or raise the minimum ports per VM instance on the NAT config. I’ve never hit this at solo-builder scale, but it’s the first thing I’d check if outbound calls started failing under load.
- One IP, one blast radius. Every service that uses this NAT gateway shares the same egress IP. If another service on the same gateway gets that IP blocklisted by a vendor or rate-limited by an API, your service inherits the problem. Keep the NAT gateway scoped to the services that actually need a static IP. Don’t route your whole platform through it because it’s convenient — that’s how one noisy service becomes everyone’s outage.
- It doesn’t make IP allowlisting good security. A static egress IP proves traffic came from your NAT gateway. It doesn’t prove it came from your service, and it doesn’t authenticate anything. Keep the API key in Secret Manager, use mutual TLS if the vendor supports it, and treat the allowlist as a network hygiene layer, not as the authentication. If the vendor offers token-based auth, take it — and then you might not need the NAT gateway at all.
When not to build this
Be honest about whether you need it:
- If the vendor supports API keys, OAuth, or mutual TLS, use those instead. A NAT gateway is infrastructure you now own — monitoring, cost, a regional dependency — in exchange for the weakest form of authentication.
- If you only need to receive traffic from a fixed source, that’s an ingress problem, not egress — Cloud Armor or ingress firewall rules, not NAT.
- If your egress volume is large, the per-GB NAT processing charge deserves a spreadsheet, not a shrug. At high volume, a dedicated egress proxy on a small Compute Engine VM with a static external IP can be cheaper — but then you own a VM, its patching, and its availability, which is a different trade.
- If you run in multiple regions, remember you’re building and paying for this per region, and handing the vendor multiple IPs. At some point the allowlist itself becomes the fragile part.
The checklist
- Reserve the static IP first; give the vendor that exact address.
- Subnet with Private Google Access enabled, so Google API traffic bypasses NAT.
- Cloud Router + Cloud NAT with — nat-external-ip-pool pointing at the reserved address, scoped to one subnet.
- Cloud Run with Direct VPC egress, all-traffic mode, in the same region.
- Verify the egress IP from inside the running service, then remove the debug endpoint.
- Alert on vendor 403s — that’s your canary for the NAT config drifting.
- Keep the gateway scoped to the services that need it, and keep real authentication (keys, mTLS) in place alongside the allowlist.
A static egress IP is a small piece of infrastructure, but it’s the kind that teaches you how GCP actually routes traffic: Cloud Run is stateless and address-less by design, your VPC is where identity lives, and NAT is the bridge between the two. Build it when a vendor forces your hand, price it honestly, and don’t let the allowlist convince you the traffic is authenticated. It’s not — it’s just coming from an address you control, which is a start, not a finish.
Give Cloud Run a Static Egress IP with Cloud NAT — and Know When Not To was originally published in Google Cloud – Community on Medium, where people are continuing the conversation by highlighting and responding to this story.
Source Credit: https://medium.com/google-cloud/give-cloud-run-a-static-egress-ip-with-cloud-nat-and-know-when-not-to-3bec7a79f1e7?source=rss—-e52cf94d98af—4
