Overview and What You Will Learn
A GCP firewall rule is defined on a VPC network and applies only to the VMs it targets, chosen by network tag, by service account, or by selecting every VM. It is not attached to a subnet. In this lab, you will create a rule that targets a tag, prove it reaches only the tagged VM, and then repeat the test with a rule that targets a service account.
Why This Matters in Production
An engineer coming from AWS or Azure writes a rule expecting it to cover every VM in a subnet, like a security group or NSG. A new VM without the matching tag is simply never selected by the rule, and nothing reports an error. Think of a Stripe-style payments team launching a new API node that quietly misses its allow rule, or worse, a node that is missing the tag that applies a deny rule.
Core Principles
GCP firewall rules live at the VPC level, and each rule has a target that decides which VMs it applies to. With no target, a rule applies to every VM in the network. With --target-tags, it applies only to VMs carrying that tag. With --target-service-accounts, it applies only to VMs running as that service account. A single rule cannot mix tags and service accounts as targets.
target-tags: web-server}} -->|tag matches| V1[vm-tagged
tag: web-server] R -.->|no match| V2[vm-untagged
no tag] V1 --> OK([Traffic allowed]) V2 --> NO([Blocked by
implied deny]) classDef rule fill:#326CE5,color:#FFFFFF classDef vm fill:#DBEAFE,color:#1E293B classDef good fill:#10B981,color:#FFFFFF classDef bad fill:#EF4444,color:#FFFFFF class R rule class V1,V2 vm class OK good class NO bad
Every VPC network also has two implied rules that you cannot delete: deny all ingress and allow all egress, both at priority 65535. A VM that no allow rule selects therefore falls through to the implied ingress deny. Rules are evaluated by priority, where a lower number wins, and new rules default to priority 1000.
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.
export PROJECT_ID=gcp-firewall-lab-mumbai-2026export ZONE=asia-south1-a- Create the project, link billing, enable the APIs, and build a VPC with one subnet:
gcloud projects create $PROJECT_ID --name="Firewall Tags Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID \ --billing-account=012345-6789AB-CDEF01gcloud services enable compute.googleapis.com iam.googleapis.com gcloud compute networks create vpc-lab --subnet-mode=customgcloud compute networks subnets create subnet-lab \ --network=vpc-lab \ --region=asia-south1 \ --range=10.0.1.0/24- Allow SSH through Identity-Aware Proxy to every VM in the network. This rule has no target, so it applies to all VMs. The VMs in this lab have no external IP, so IAP is how you reach them:
gcloud compute firewall-rules create allow-iap-ssh \ --network=vpc-lab \ --direction=INGRESS \ --action=ALLOW \ --rules=tcp:22 \ --source-ranges=35.235.240.0/20- Create a rule that allows port 8080 from inside the subnet, but only to VMs tagged
web-server. Port 8080 stands in for a web port so the lab can run without certificates:
gcloud compute firewall-rules create allow-web-by-tag \ --network=vpc-lab \ --direction=INGRESS \ --action=ALLOW \ --rules=tcp:8080 \ --source-ranges=10.0.1.0/24 \ --target-tags=web-server- Create three VMs: one with the tag, one without it, and a client to run the tests from:
gcloud compute instances create vm-tagged \ --zone=$ZONE --machine-type=e2-small \ --image-family=debian-12 --image-project=debian-cloud \ --network=vpc-lab --subnet=subnet-lab \ --no-address --tags=web-server gcloud compute instances create vm-untagged \ --zone=$ZONE --machine-type=e2-small \ --image-family=debian-12 --image-project=debian-cloud \ --network=vpc-lab --subnet=subnet-lab \ --no-address gcloud compute instances create vm-client \ --zone=$ZONE --machine-type=e2-small \ --image-family=debian-12 --image-project=debian-cloud \ --network=vpc-lab --subnet=subnet-lab \ --no-address- Confirm which tags each VM actually carries:
gcloud compute instances describe vm-tagged \ --zone=$ZONE --format="value(tags.items)" gcloud compute instances describe vm-untagged \ --zone=$ZONE --format="value(tags.items)"- Start a simple web server on port 8080 on both target VMs, using a systemd transient unit so it keeps running after the SSH session ends:
for VM in vm-tagged vm-untagged; do gcloud compute ssh $VM --zone=$ZONE --tunnel-through-iap \ --command="sudo systemd-run --unit=demo-web python3 -m http.server 8080"done- Test from the client. The tagged VM should return
200. The untagged VM should print000after the timeout, because the allow rule never selected it:
export TAGGED_IP=$(gcloud compute instances describe vm-tagged \ --zone=$ZONE --format="value(networkInterfaces[0].networkIP)")export UNTAGGED_IP=$(gcloud compute instances describe vm-untagged \ --zone=$ZONE --format="value(networkInterfaces[0].networkIP)") gcloud compute ssh vm-client --zone=$ZONE --tunnel-through-iap \ --command="curl -s -m 5 -o /dev/null -w '%{http_code}' http://$TAGGED_IP:8080; echo; curl -s -m 5 -o /dev/null -w '%{http_code}' http://$UNTAGGED_IP:8080; echo"- Now target a service account instead. Create the account, a rule that targets it, and a VM that runs as it:
gcloud iam service-accounts create web-server-sa gcloud compute firewall-rules create allow-web-by-sa \ --network=vpc-lab \ --direction=INGRESS \ --action=ALLOW \ --rules=tcp:8080 \ --source-ranges=10.0.1.0/24 \ --target-service-accounts=web-server-sa@$PROJECT_ID.iam.gserviceaccount.com gcloud compute instances create vm-sa \ --zone=$ZONE --machine-type=e2-small \ --image-family=debian-12 --image-project=debian-cloud \ --network=vpc-lab --subnet=subnet-lab \ --no-address \ --service-account=web-server-sa@$PROJECT_ID.iam.gserviceaccount.com- Start the web server on
vm-saand test it from the client. It has no tag, so only the service account rule can be letting it through:
gcloud compute ssh vm-sa --zone=$ZONE --tunnel-through-iap \ --command="sudo systemd-run --unit=demo-web python3 -m http.server 8080" export SA_IP=$(gcloud compute instances describe vm-sa \ --zone=$ZONE --format="value(networkInterfaces[0].networkIP)") gcloud compute ssh vm-client --zone=$ZONE --tunnel-through-iap \ --command="curl -s -m 5 -o /dev/null -w '%{http_code}' http://$SA_IP:8080; echo"- Clean up:
gcloud compute instances delete vm-tagged vm-untagged vm-client vm-sa \ --zone=$ZONE --quietgcloud compute firewall-rules delete allow-iap-ssh allow-web-by-tag allow-web-by-sa --quietgcloud compute networks subnets delete subnet-lab --region=asia-south1 --quietgcloud compute networks delete vpc-lab --quietgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeAssuming a rule with
--target-tagsor--target-service-accountscovers every VM in the subnet. Any VM without that exact tag or identity is silently unaffected, so a new VM can launch with no allow rule, or with no deny rule you meant it to have.
SecurityAnyone with permission to edit an instance's tags can change which firewall rules apply to it. Tags are free-form strings with no IAM check on their meaning. For security-critical rules, target service accounts, or use secure tags with network firewall policies.
TipPrefer service account targeting for production tiers. A service account is a real IAM identity attached at VM creation, it appears in audits, and it cannot be mistyped the way a tag can.
- Priority decides conflicts. A lower number wins, so a specific deny at priority 500 overrides a broader allow at priority 1000.
- Egress works the same way. An egress rule's targets are the VMs sending the traffic, selected by their own tag or service account.
- Avoid rules with no target. A rule with no target tags or service accounts applies to every VM in the network, including ones created later.
- Name tags like an interface. Agree on a short list such as
web-serveranddb-server, and document it, since nothing validates tag spelling.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute firewall-rules create --target-tags=TAG |
Create a rule scoped to tagged VMs |
gcloud compute firewall-rules create --target-service-accounts=EMAIL |
Create a rule scoped to a service account |
gcloud compute instances create --tags=TAG |
Assign network tags at creation |
gcloud compute instances add-tags VM --tags=TAG |
Add a tag to an existing VM |
gcloud compute instances describe VM --format="value(tags.items)" |
Check a VM's current tags |
gcloud compute firewall-rules list --filter="network:vpc-lab" |
List all rules on a network with priorities |