> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloud Firewall Rules

> Overview of Google Cloud VPC firewall rules, their components, configuration examples, best practices and troubleshooting guidance for data engineers securing and managing network access to instances

In this lesson we cover Cloud firewall rules in Google Cloud Platform (GCP): what they are, how they’re defined, and why they matter for data engineers. Firewalls act like digital bouncers for your virtual machines—deciding who can connect, who is blocked, and under what conditions. In GCP, firewall rules live in a VPC network and control traffic to and from instances in that VPC.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Ba15d4EpA0h-eMDb/images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/cloud-firewall-rules-access-conditions.jpg?fit=max&auto=format&n=Ba15d4EpA0h-eMDb&q=85&s=26ef330e711d7ef86c1bb7fbe52e85d6" alt="An infographic slide titled &#x22;Cloud Firewall Rules&#x22; showing a dashed boundary around server/database icons protected by a shield checkmark. To the right are three colored icons with labels: &#x22;Who gets in,&#x22; &#x22;Who stays out,&#x22; and &#x22;Under what conditions.&#x22;" width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/cloud-firewall-rules-access-conditions.jpg" />
</Frame>

Why this matters for data engineers

* When connecting to a database, message broker, or other data service, network teams will ask which ports, source CIDR ranges, or service accounts require access.
* Knowing how VPC firewall rules work lets you provide precise requirements, speed up approvals, and troubleshoot connectivity issues faster.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Ba15d4EpA0h-eMDb/images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/data-engineer-gcp-ports-firewall-database.jpg?fit=max&auto=format&n=Ba15d4EpA0h-eMDb&q=85&s=89f6c2054aff2ff370e429e2dc38550a" alt="An illustration titled &#x22;Why This Matters for Data Engineers&#x22; showing a person at a laptop with speech bubbles asking about ports and firewall rules, connected through a GCP cloud icon to a database icon." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/data-engineer-gcp-ports-firewall-database.jpg" />
</Frame>

Core concepts — how a firewall rule is defined

When you create a firewall rule in a VPC you specify several properties. The table below summarizes each key field, what it controls, and an example.

| Component            | What it controls                                                                      | Example                                                               |
| -------------------- | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| Direction            | Whether the rule inspects entering or leaving traffic                                 | `INGRESS` or `EGRESS`                                                 |
| Action               | Permit or block matching traffic                                                      | `allow` or `deny`                                                     |
| Targets              | Which instances the rule applies to                                                   | `target-tags: db-server` or instances with a specific service account |
| Source / Destination | Where traffic originates (ingress) or goes (egress) — CIDR, tags, or service accounts | `--source-ranges 10.1.0.0/16`                                         |
| Protocols & ports    | Protocols (TCP/UDP/ICMP) and port ranges allowed or denied                            | `--allow tcp:5432`                                                    |
| Priority             | Numeric order for rule evaluation — lower = higher priority                           | `--priority 1000`                                                     |

1. Direction

* Ingress: traffic entering instances in the VPC (incoming).
* Egress: traffic leaving instances in the VPC (outgoing).

2. Action

* Allow: permit the matching traffic.
* Deny: block the matching traffic.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Ba15d4EpA0h-eMDb/images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/configuring-vpc-firewall-rules.jpg?fit=max&auto=format&n=Ba15d4EpA0h-eMDb&q=85&s=6cb757a0b93f1e827aaa8244a45d832f" alt="A presentation slide titled &#x22;Configuring VPC Firewall Rules&#x22; with a teal VPC cloud icon on the left. The slide lists two steps: &#x22;Define Rule Direction&#x22; (ingress/egress) and &#x22;Set Rule Action&#x22; (allow or deny traffic)." width="1920" height="1080" data-path="images/Google-Cloud-Professional-Data-Engineer-Certification/GCP-Networking/Cloud-Firewall-Rules/configuring-vpc-firewall-rules.jpg" />
</Frame>

3. Targets

* Apply rules to:
  * All instances in the network (no target specification).
  * Instances with specific network tags (e.g., `frontend`, `db-server`).
  * Instances running under specific service accounts.
* Use targets to scope rules only to the intended VMs and reduce blast radius.

4. Source and destination

* Specify where traffic comes from (for ingress) or goes to (for egress):
  * IP ranges using CIDR (e.g., `10.0.0.0/8`, `0.0.0.0/0`).
  * Source tags or service accounts (useful for internal VPC communications).
* This controls which systems are allowed to talk to each other.

5. Protocols and ports

* Specify allowed or denied protocols (TCP, UDP, ICMP) and port ranges.
* Common examples:
  * `tcp:5432` for PostgreSQL
  * `tcp:22` for SSH

6. Priority

* Numeric value where lower numbers have higher priority.
* GCP evaluates rules starting with the lowest numeric priority and stops when a matching rule explicitly allows or denies the traffic. If multiple matching rules share the same priority, DENY rules take precedence over ALLOW rules.

<Callout icon="lightbulb" color="#1CB2FE">
  When creating or requesting firewall changes, be ready to provide: direction, action, targets (`tags` or service accounts), source/destination ranges, allowed/denied protocols and ports, and the desired priority.
</Callout>

Example: allow inbound PostgreSQL (TCP 5432) from a specific CIDR to VMs tagged `db-server`

```bash theme={null}
gcloud compute firewall-rules create allow-postgres-ingress \
  --network my-vpc \
  --direction INGRESS \
  --priority 1000 \
  --allow tcp:5432 \
  --source-ranges 10.1.0.0/16 \
  --target-tags db-server \
  --description "Allow Postgres from internal network to db servers"
```

Common best practices and tips

* Principle of least privilege: allow only the ports and source ranges that are required.
* Prefer network tags or service accounts for targets (instead of broad all-instances rules).
* Use specific CIDR ranges; avoid `0.0.0.0/0` unless absolutely necessary and documented.
* Use priorities to ensure more specific rules take effect before broad ones.
* Test changes in a staging VPC before applying to production.

<Callout icon="warning" color="#FF6B6B">
  Be cautious when using `0.0.0.0/0` as a source or destination. Broad exposure increases security risk—document and justify any open ranges.
</Callout>

Notes on permissions and organizational roles

* Many organizations restrict who can create or modify firewall rules. Data engineers often need to request changes from network or cloud operations teams.
* Providing a clear, minimal set of requirements reduces back-and-forth and speeds approvals:
  * Which ports and protocols (e.g., `tcp:5432`)
  * Source CIDR(s) or source tags/service accounts
  * Target tags or service account for the VM(s)
  * Direction (`INGRESS`/`EGRESS`)
  * Desired `priority` and a short `description`

Troubleshooting checklist

* Confirm the VM has the expected network tag or service account.
* Verify the rule priority and whether another rule is blocking traffic.
* Check that the GCE instance-level firewall (iptables) or application firewall isn’t blocking traffic.
* Use `gcloud compute firewall-rules list` and `gcloud compute firewall-rules describe <NAME>` to inspect rules.
* Test connectivity with `telnet <host> <port>` or `nc -vz <host> <port>` from an allowed source.

References and further reading

* [VPC firewall rules — GCP Documentation](https://cloud.google.com/vpc/docs/firewalls)
* [gcloud compute firewall-rules](https://cloud.google.com/sdk/gcloud/reference/compute/firewall-rules)

That covers the essentials of VPC firewall rules and why they matter for data engineers. Understanding these elements will help you design secure connections between your data sources and sinks.

That concludes this article.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/google-cloud-professional-data-engineer-certification/module/f2509a3a-1b7e-49f6-bdea-4985dc552c0e/lesson/b842c390-bcc5-4180-9899-18e491a2c523" />
</CardGroup>
