Overview and What You Will Learn
Cloud NAT gives Google Cloud VMs that have no external IP address outbound access to the internet, while blocking every connection that starts from the internet side. It only serves VMs without external IPs, and it never allows inbound connections.
In this lab, you will:
- Create a VPC, subnet and VM with no external IP
- Reach the private VM over SSH with Identity-Aware Proxy (IAP)
- Prove the VM cannot reach the internet, then add a Cloud Router and a Cloud NAT gateway
- Verify outbound access works while inbound exposure stays at zero
- Read NAT logs to troubleshoot dropped connections
Why This Matters in Production
Cloud NAT lets private workloads such as database VMs fetch OS security patches without a public IP, which removes the easiest path for anyone on the internet to reach them. Picture a platform team at a payments company like Razorpay: the database VMs must pull monthly patches, but a public IP "just for updates" would also expose SSH and database ports to the whole internet. With Cloud NAT the VMs get outbound reach and nothing else, so the security review has one less finding.
Core Principles
Cloud NAT is a managed, outbound-only address translation service, the GCP counterpart of an AWS NAT Gateway or an Azure NAT Gateway. Private VMs can start connections out, replies are allowed back along the same connection, and no new connection can be started from the internet. It is software-defined rather than a VM you run, so there is no single instance to size or patch.
no external IP]) --> RT[Cloud Router] RT --> NAT[Cloud NAT gateway] NAT -->|Outbound only| NET{{Internet}} NET -.->|Replies only| NAT classDef app fill:#DBEAFE,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef cloud fill:#EDE9FE,color:#1E293B class VM app class RT,NAT tool class NET cloud
A Cloud NAT gateway always attaches to a Cloud Router in the same region and the same VPC. In a simple setup the router exchanges no routes at all, because it is simply the resource that holds the NAT configuration.
Detailed Step-by-Step Practical Lab
Set the variables used throughout the lab. Project IDs are globally unique, so change the suffix if the create command fails, and use your own Billing Account ID.
export PROJECT_ID=mumbai-nat-lab-2026export BILLING_ACCOUNT_ID=012345-6789AB-CDEF01- Create the project, link billing, and build a VPC with one subnet in Mumbai:
gcloud projects create $PROJECT_ID --name="Cloud NAT Lab"gcloud billing projects link $PROJECT_ID --billing-account=$BILLING_ACCOUNT_IDgcloud config set project $PROJECT_IDgcloud services enable compute.googleapis.com gcloud compute networks create vpc-nat-lab --subnet-mode=customgcloud compute networks subnets create subnet-nat-lab \ --network=vpc-nat-lab \ --region=asia-south1 \ --range=10.0.1.0/24- Allow SSH from the IAP range only, since a VM with no external IP cannot be reached directly:
gcloud compute firewall-rules create allow-iap-ssh \ --network=vpc-nat-lab \ --direction=INGRESS \ --allow=tcp:22 \ --source-ranges=35.235.240.0/20- Create a VM with no external IP address:
gcloud compute instances create mumbai-prod-vm-1 \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud \ --network=vpc-nat-lab \ --subnet=subnet-nat-lab \ --no-address- Test internet access through IAP before NAT exists. The command prints the HTTP status code, or
000if the request fails:
gcloud compute ssh mumbai-prod-vm-1 \ --zone=asia-south1-a \ --tunnel-through-iap \ --command='curl -sS -m 5 -o /dev/null -w "%{http_code}\n" https://www.google.com'Expected result: a timeout error and 000, because no route to the internet exists yet.
- Create the Cloud Router that the NAT gateway attaches to:
gcloud compute routers create router-nat-lab \ --network=vpc-nat-lab \ --region=asia-south1- Create the Cloud NAT gateway on that router. Logging is limited to errors, which keeps log volume low:
gcloud compute routers nats create nat-config-lab \ --router=router-nat-lab \ --region=asia-south1 \ --auto-allocate-nat-external-ips \ --nat-all-subnet-ip-ranges \ --enable-logging \ --log-filter=ERRORS_ONLY- Repeat the test. Allow about a minute for the gateway to become active:
gcloud compute ssh mumbai-prod-vm-1 \ --zone=asia-south1-a \ --tunnel-through-iap \ --command='curl -sS -m 5 -o /dev/null -w "%{http_code}\n" https://www.google.com'Expected result: 200. Outbound works even though the VM has no external IP.
- Confirm the VM still has no public address, so nothing can start a connection to it:
gcloud compute instances describe mumbai-prod-vm-1 \ --zone=asia-south1-a \ --format="value(networkInterfaces[0].accessConfigs[0].natIP)"Expected result: empty output, meaning no external IP.
- Inspect the gateway and read its logs. With
ERRORS_ONLY, healthy traffic produces no entries:
gcloud compute routers nats describe nat-config-lab \ --router=router-nat-lab --region=asia-south1 gcloud logging read 'resource.type="nat_gateway"' --limit=5- Clean up in reverse order of creation:
gcloud compute instances delete mumbai-prod-vm-1 --zone=asia-south1-a --quietgcloud compute routers nats delete nat-config-lab --router=router-nat-lab --region=asia-south1 --quietgcloud compute routers delete router-nat-lab --region=asia-south1 --quietgcloud compute firewall-rules delete allow-iap-ssh --quietgcloud compute networks subnets delete subnet-nat-lab --region=asia-south1 --quietgcloud compute networks delete vpc-nat-lab --quietgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeGiving a private VM a public IP only so it can download patches. Cloud NAT gives the same outbound access with zero inbound exposure, and a public IP added "just for updates" is rarely removed afterwards.
Tip
--nat-all-subnet-ip-rangescovers every subnet in the region. When only some workloads should reach the internet, use--nat-custom-subnet-ip-rangesto pick specific subnets instead.
RememberCloud NAT is regional. A VM in
us-central1needs its own gateway and router inus-central1, even inside the same global VPC.
- Reserve static NAT IPs when partners allowlist you. Automatic allocation can change as traffic grows, so use
--nat-external-ip-poolwith reserved addresses when a bank or SaaS vendor must allowlist your egress IPs. - Watch for port exhaustion. Each VM gets a minimum number of NAT ports, and many parallel outbound connections to one destination can exhaust them, so raise
--min-ports-per-vmor add NAT IPs. - Enable logging before you need it. NAT logs show dropped connections and allocation failures, which is the fastest way to debug "it worked yesterday" outbound issues.
- Use Private Google Access for Google APIs. Traffic to Cloud Storage or Artifact Registry does not need NAT when Private Google Access is enabled on the subnet.
- Remember the cost model. NAT is billed per gateway hour plus data processed, so avoid routing very large transfers through it when a private path exists.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute routers create |
Create the Cloud Router the NAT attaches to |
gcloud compute routers nats create |
Configure a Cloud NAT gateway on a router |
gcloud compute routers nats describe |
Inspect NAT IP allocation and port settings |
gcloud compute instances create --no-address |
Create a VM with no external IP |
gcloud compute ssh --tunnel-through-iap |
SSH into a private VM through IAP |
gcloud compute routers get-status |
Check NAT status and allocated IPs on a router |
gcloud compute networks subnets update --enable-private-ip-google-access |
Reach Google APIs without NAT |