Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 45 ยท Networking

VPC Networks, Subnets and Firewall Rules

Learn Google Cloud VPC networks: custom vs auto mode, regional subnets, routes, firewall rule priorities, network tags and Private Google Access, with simulators.

Every virtual machine, GKE cluster and many managed services live on a network. In Google Cloud that network is a Virtual Private Cloud (VPC): a private, software-defined network that belongs to your project. It decides which addresses your resources get, which traffic may flow between them, and how they reach the internet.

This lesson covers the building blocks you meet first: networks, subnets, routes and firewall rules. Defaults, quotas and exact behaviors change, so verify details in the current documentation before relying on them.

The pieces of a VPC

A VPC in four parts

Network Global container for your private network.
Subnets Regional IP ranges where resources live.
Routes Where packets go next.
Firewall rules Which traffic is allowed.

The network spans the world; subnets belong to one region.

1 / 2

A VPC network is global: one network can contain subnets in many regions, and VMs in different regions on the same network can talk over private addresses, with no VPN needed. A subnet is regional: it is an IP range, such as 10.10.0.0/24, in one region, and it can span all zones of that region. A VM gets its internal IP from the subnet in the region where it runs.

Custom mode vs auto mode

  • Auto mode creates one subnet in every region automatically, with predefined ranges. It is quick for experiments, but the ranges may overlap with your other networks, which blocks connecting them later.
  • Custom mode starts empty. You create the subnets you want with ranges you choose. This is the recommended choice for production.
  • New projects may come with a default network that is auto mode with permissive example firewall rules. Many teams delete it or disable its creation by policy (Least Privilege and Organization Policies).
Terminal
# Create a custom-mode network, one subnet, and a firewall rule (read-only example)
gcloud compute networks create shop-net --subnet-mode=custom

gcloud compute networks subnets create shop-web \
  --network=shop-net --region=europe-west1 --range=10.10.0.0/24

gcloud compute firewall-rules create allow-https \
  --network=shop-net --direction=INGRESS --action=ALLOW \
  --rules=tcp:443 --source-ranges=0.0.0.0/0 --target-tags=web

Reading a CIDR range

10.10.0.0/24 means the first 24 of 32 bits are the network part; the remaining 8 bits can vary, so there are 2^8 = 256 addresses. Google reserves 4 addresses in every subnet's primary range (the network address, the gateway, and two more), so a /24 provides 252 usable addresses. You can expand a subnet's range later but not shrink it, so leave room to grow, and never overlap ranges you may connect (Hybrid Connectivity, VPN and Peering).

Routes

A route says "traffic for this destination range goes to that next hop." Every network automatically has system-generated routes: one for each subnet (so internal traffic works), and a default route 0.0.0.0/0 to the internet gateway. The most specific destination (longest prefix) wins; with equal prefixes, a lower priority number wins. A VM with no external IP cannot use the internet route, since the internet gateway only serves instances with an external address (or Cloud NAT).

  • To let VMs without external IPs reach the internet for updates, use Cloud NAT.
  • To let them reach Google APIs (Cloud Storage, BigQuery) without any external IP, turn on Private Google Access for the subnet.

Firewall rules

Firewall rules are stateful: when a connection is allowed in one direction, the reply is allowed automatically. They apply to the VM's network interface, not only the network edge, so two VMs in the same subnet are still filtered.

A rule has:

  • Direction: ingress (into the VM) or egress (out of it).
  • Action: allow or deny.
  • Priority: a number from 0 to 65535; lower number = higher priority. The first matching rule by priority decides. For rules with the same priority, deny beats allow.
  • Match: protocols and ports, plus a source (ingress) or destination (egress) such as an IP range, and the targets it protects.

Every network has two implied rules at the lowest priority (65535): deny all ingress and allow all egress. So by default nothing can come in unless you allow it, and everything can go out.

Targets: tags and service accounts

Targets decide which VMs a rule applies to. Network tags are simple strings on a VM (for example web). Anyone who can edit the VM can add a tag, so tags are not a strong security boundary. Service accounts as targets are stronger, because changing a VM's service account needs permission (Service Accounts and Credentials). Secure tags (resource-manager tags) are a newer IAM-controlled option. Check the current documentation for which target types your rule type supports.

A common design: allow HTTPS from the internet to VMs tagged web; allow port 5432 from web to VMs tagged db; and allow SSH only through Identity-Aware Proxy rather than from 0.0.0.0/0.

Stand-ins: a CIDR calculator and a firewall matcher

Google evaluates firewall rules in its network stack. The two programs below only model the arithmetic and the priority logic, so you can see how a decision comes out. The packets and rules are invented.

JS CIDR calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Firewall rule matcher

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real and what is the stand-in: the gcloud commands, CIDR math, the 4 reserved addresses, priority rules and implied rules reflect real VPC behavior. The programs are simplified models: real rules also match protocols, source ranges (not just labels) and egress, and Google evaluates them for you.

More VPC features and benefits

Think of a VPC as the floor plan of a private office building. The network is the building, subnets are the office rooms, and routes and peering are the hallways between them. Firewalls are the doors with badge readers. Because one VPC can span every region, a global company can have one building instead of a campus of separate ones.

What this design gives you: it is beginner-friendly (auto mode creates a working network in seconds), secure by default (ingress denied until you allow it, private IPs, Private Google Access), and cost-efficient (traffic inside a zone is free, and the global network avoids a mesh of VPN tunnels).

Features beyond the basics:

  • Packet Mirroring clones the traffic of chosen VMs (by subnet, tag or instance) to a collector such as an intrusion-detection appliance, without touching the original traffic. It serves security monitoring and troubleshooting. The collector sits behind an internal load balancer.
  • Network Intelligence Center is a set of diagnostic tools: Connectivity Tests (will a packet from A reach B, and which rule stops it?), a Network Topology view, Performance Dashboard and Firewall Insights (which rules are never used, which are shadowed).
  • VPC Flow Logs record sampled connections for analysis and cost investigation.
  • Shared VPC and peering connect networks across projects (Hybrid Connectivity).

Creating and deleting a custom network

Terminal
gcloud compute networks create demo-net --subnet-mode=custom
gcloud compute networks subnets create demo-subnet \
  --network=demo-net --region=us-central1 --range=10.10.0.0/24
gcloud compute firewall-rules create demo-allow-ssh \
  --network=demo-net --allow=tcp:22 --source-ranges=35.235.240.0/20

Deleting goes in the reverse order, because a network cannot be deleted while things still depend on it. Remove the VMs and other resources first, then the firewall rules and routes you created, then the subnets, and finally the network:

Terminal
gcloud compute firewall-rules delete demo-allow-ssh
gcloud compute networks subnets delete demo-subnet --region=us-central1
gcloud compute networks delete demo-net

In the console the same actions live under VPC network, VPC networks: choose Create VPC network to build one, or select it and choose Delete VPC network. The console tells you which resources still block a deletion.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the scope of a subnet in a Google Cloud VPC?

    The VPC network is global, but each subnet belongs to one region and spans all of its zones.
  2. 2. Two ingress firewall rules match a packet: an allow with priority 1000 and a deny with priority 900. What happens?

    Lower numbers are evaluated first. Only on equal priority does deny take precedence over allow.
  3. 3. A VM has no external IP. How can it reach Cloud Storage privately?

    Private Google Access lets VMs without external IPs reach Google APIs and services through internal paths.
  4. 4. Which is the main weakness of network tags as firewall targets?

    Tags are not IAM-protected like service accounts or secure tags, so they are weaker as a security boundary.

Summary

  • A VPC network is global; subnets are regional IP ranges; routes and firewall rules control traffic.
  • Prefer custom-mode networks with ranges you plan, and avoid overlapping ranges you may connect later.
  • Firewall rules are stateful, have priorities (lower number wins), and the implied rules deny ingress and allow egress.
  • Target rules with service accounts or secure tags rather than plain tags when security matters.
  • Use Cloud NAT for outbound internet and Private Google Access for Google APIs without external IPs.

Next: Load Balancing, Cloud CDN and DNS