Skip to content
Visual explainer 3h to learn 5 steps

Design a Google Cloud VPC with subnets and firewall rules

Plan a global VPC with regional subnets, write firewall rules, prove reachability and choose between Cloud NAT and Private Google Access.

By Invicly Tech, , in Google Cloud Engineering and Services. About 10 minutes to read.

Topic 7 of 32

In short

A Google Cloud VPC network is one private network that spans every region. You cut it into regional subnets, control traffic with firewall rules, and decide how private machines reach the internet and Google APIs. Good design on day one saves painful re-addressing later.

Think of it like this: A VPC is like a company campus: one fence around everything, separate buildings (subnets) in each city, and guards (firewall rules) at the doors.

Explained simply

Think of your cloud network as a private neighbourhood. In Google Cloud, one VPC (virtual private cloud) is a single neighbourhood that covers the whole world. Inside it you build subnets, which are blocks of addresses tied to one region, such as Iowa or Belgium.

Every virtual machine gets an address from a subnet. Machines in the same VPC can talk to each other privately, even across regions, as long as firewall rules allow it. You do not need to connect regions yourself.

A firewall rule says who may talk to whom. By default Google Cloud blocks all incoming traffic and allows all outgoing traffic. So you add rules to open only what you need, like SSH from a trusted range.

Many machines should have no public address at all. They still need to download updates. Cloud NAT lets them start outgoing connections to the internet. Private Google Access is a smaller tool: it only lets them reach Google services such as Cloud Storage.

Press Play on a diagram to walk through it one step at a time.

MapOne global VPC, regional subnets
VPC network (global)
  • Subnet in us-central1
    • 10.10.0.0/20
    • VMs in zones a, b, c
  • Subnet in europe-west1
    • 10.20.0.0/20
    • VMs in zones b, c, d
  • Firewall rules (VPC-wide)
    • Priority 0 to 65535
    • Target by tag or service account
  • Routes (VPC-wide)
    • Subnet routes
    • Default route to internet gateway
CompareTwo ways for private VMs to reach outside
Cloud NAT
  • For general internet egress
  • Needs a Cloud Router in the region
  • VMs need no external IP
  • Only allows replies to outbound connections
Private Google Access
  • For Google APIs and services only
  • Switched on per subnet
  • VMs need no external IP
  • No NAT gateway to run
LayersFrom a VM to the internet without a public IP
  1. 1
    VM with internal IP only

    10.10.0.5 in a regional subnet

  2. 2
    Subnet and routes

    Default route 0.0.0.0/0 exists

  3. 3
    Firewall egress rules

    Implied allow, unless you added denies

  4. 4
    Cloud NAT on a Cloud Router

    Translates to a NAT external IP

  5. 5
    Internet

    Sees only the NAT address

In depth

Planning subnet ranges

Pick ranges from private space such as 10.0.0.0/8, and give each region and environment its own block. Leave gaps. You can expand a subnet's primary range later, but you cannot shrink it or move it to overlap another. Ranges also must not overlap with any network you will connect later by VPN or peering. In each primary range, Google reserves four addresses: the first two and the last two. GKE and some services also use secondary ranges for Pods and Services, so plan for those.

  • Use custom mode, not auto mode, for real projects.
  • Give production and test different non-overlapping blocks.
  • Write the plan down before you create anything.

How firewall rules are evaluated

Each rule has a direction, a priority from 0 to 65535, an action, a protocol and ports, and a source or destination. It applies to targets: all instances, instances with a network tag, or instances running as a given service account. Lower numbers win. Rules are stateful, so replies to allowed traffic are allowed automatically. Two implied rules sit at priority 65535: allow all egress and deny all ingress. Service accounts are a stronger target than tags, because tags can be set by anyone who can edit the VM.

FlowHow one packet meets the rules
  1. 1
    Packet arrives

    Direction and target VM known

  2. 2
    Collect matching rules

    By target, source and port

  3. 3
    Sort by priority

    Lowest number first

  4. 4
    First match decides

    Deny wins on a tie

  5. 5
    Implied rule if none

    Deny ingress, allow egress

Routes and testing

A route tells the network where to send packets for a destination range. Subnet routes are created for you. A default route (0.0.0.0/0) points at the default internet gateway. A VM without an external IP cannot use that gateway to reach the internet, which is why Cloud NAT exists. To test reachability without logging in to VMs, use Connectivity Tests in Network Intelligence Center. It analyses the configuration and tells you which rule or route drops the packet. Firewall Rules Logging shows what actually happened on live traffic.

Cloud NAT or Private Google Access

Cloud NAT is a regional, software-defined service that runs on top of a Cloud Router. It translates private source addresses to external addresses for outgoing connections, and only lets replies back. Private Google Access is a per-subnet switch. It lets VMs with only internal IPs reach the public Google API addresses. Many designs use both: Private Google Access for Cloud Storage and APIs, Cloud NAT for package repositories and third-party APIs. Remember that NAT costs money per gateway hour and per data processed.

  • Cloud NAT: needs a router, covers general internet egress.
  • Private Google Access: no router, Google APIs only.
  • Neither allows unsolicited inbound connections from the internet.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Plan subnet ranges per region~35m
  2. 2
    Write ingress and egress firewall rules~35m
  3. 3
    Read routes and test connectivity~35m
  4. 4
    Give private VMs egress with Cloud NAT~35m
  5. 5
    Reach Google APIs with Private Google Access~35m

Key points

  • A VPC is global; a subnet belongs to one region and spans all its zones.
  • Use custom mode subnets and pick ranges that do not overlap with other networks you may connect later.
  • Firewall rules are stateful. Lower priority number wins. The implied rules deny all ingress and allow all egress.
  • Cloud NAT gives egress for VMs without external IPs. Private Google Access only reaches Google APIs.
  • Test with Connectivity Tests and firewall logs instead of guessing.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
gcloud compute networks create demo-vpc --subnet-mode=custom
gcloud compute networks subnets create demo-us --network=demo-vpc \
  --region=us-central1 --range=10.10.0.0/20 --enable-private-ip-google-access
gcloud compute firewall-rules create demo-allow-iap-ssh --network=demo-vpc \
  --direction=INGRESS --action=ALLOW --rules=tcp:22 \
  --source-ranges=35.235.240.0/20 --target-tags=admin
gcloud compute routers create demo-router --network=demo-vpc --region=us-central1
gcloud compute routers nats create demo-nat --router=demo-router \
  --region=us-central1 --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges

Custom VPC, one subnet, an SSH-through-IAP rule and Cloud NAT. Names and ranges are placeholders.

Plan two regions and open only IAP SSH

You need a test network in two regions. Create a custom VPC, one subnet per region from separate ranges, and a single ingress rule that lets only Google's IAP range reach SSH on VMs tagged admin. Everything else stays denied by the implied rule. Delete the resources when done to avoid cost.

bash
gcloud compute networks create lab-vpc --subnet-mode=custom
gcloud compute networks subnets create lab-us --network=lab-vpc \
  --region=us-central1 --range=10.10.0.0/20
gcloud compute networks subnets create lab-eu --network=lab-vpc \
  --region=europe-west1 --range=10.20.0.0/20
gcloud compute firewall-rules create lab-iap-ssh --network=lab-vpc \
  --allow=tcp:22 --source-ranges=35.235.240.0/20 --target-tags=admin

Give a private VM package updates

A VM with no external IP cannot run apt update. Add a Cloud Router and a Cloud NAT gateway in its region. The evidence is that apt update now succeeds and the NAT gateway logs show translations. Rollback is deleting the NAT and the router.

bash
gcloud compute routers create lab-router --network=lab-vpc --region=us-central1
gcloud compute routers nats create lab-nat --router=lab-router \
  --region=us-central1 --auto-allocate-nat-external-ips \
  --nat-all-subnet-ip-ranges
gcloud compute routers get-nat-mapping-info lab-router --region=us-central1

Quick reference

  • VPC is global; subnets are regional and span all zones of that region.
  • Firewall: lower priority number wins; default 1000; implied rules sit at 65535.
  • Implied rules: deny all ingress, allow all egress.
  • IAP TCP forwarding source range: 35.235.240.0/20.
  • Cloud NAT needs a Cloud Router and is regional; no VM external IP needed.
  • Private Google Access is set per subnet and only reaches Google APIs.
  • Connectivity Tests: analyse config without sending traffic.
  • Use custom mode subnets; avoid the default network in production.

Common mistakes

  • Leaving the default network or auto mode subnets in place, then finding the ranges overlap with your office or another cloud.
  • Expecting Private Google Access to give general internet access. It only reaches Google APIs.
  • Forgetting that the implied deny-ingress rule is why a new VM cannot be reached until you add an allow rule.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which statement about a Google Cloud VPC is true?

Question 2

2A new VM cannot be reached on port 80. What is the most likely reason?

Question 3

3Which rule applies when priority 500 deny and priority 1000 allow both match?

Question 4

4A VM with only an internal IP must call a third-party API. What do you use?

Question 5

5Which is the best way to check why a packet is dropped without logging in to VMs?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Write a plan for two non-overlapping /20 subnets, one in us-central1 and one in europe-west1.

  2. Exercise 2medium

    Write a firewall rule that allows HTTP from anywhere only to VMs tagged web.

  3. Exercise 3hard

    A private VM can reach Cloud Storage but not example.com. List the two things to check and fix.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use the default network?
Not for real workloads. It has broad preset rules and auto mode ranges that can overlap other networks. Create a custom mode VPC.
Do I need a public IP for each VM?
No. Use internal IPs and add Cloud NAT, Private Google Access or a load balancer for what you need.
Can I change a subnet range later?
You can expand a primary range, but you cannot shrink it or change it to overlap another one. Plan carefully.

Go deeper

ShareXLinkedInWhatsApp