Enterprise VPC Networking on Google Cloud
This Terraform module creates and manages a complete VPC networking stack on Google Cloud Platform. It provisions custom VPC networks with configurable routing mode and MTU, subnets with secondary IP ranges (for GKE pods/services), Private Google Access, VPC Flow Logs, Cloud NAT with Cloud Router for outbound internet access, firewall rules with allow/deny and target tags or service accounts, Shared VPC host/service project configuration, private DNS zones, and VPC Access Connectors for serverless services. The module serves as the foundational networking layer for all other GCP infrastructure.
VPC, Subnets, Router, NAT, Firewall, DNS, Shared VPC, VPC Connector
REGIONAL and GLOBAL routing
Network, subnets, firewall, NAT, DNS, Shared VPC, tags
Network, subnet, router, NAT, firewall, DNS IDs and links
google_compute_network)Custom mode VPC with configurable auto-create subnetworks (default: false), routing mode (REGIONAL/GLOBAL, validated), MTU (1300-8896, validated), and optional deletion of default 0.0.0.0/0 routes.
google_compute_subnetwork)Created via for_each from a list of subnet objects. Each supports primary CIDR range, secondary IP ranges for GKE pods/services, Private Google Access per subnet, and VPC Flow Logs with 5-second intervals and 50% sampling.
google_compute_router_nat)Cloud Router with BGP ASN 64514 paired with Cloud NAT gateway. Configurable: IP allocation (AUTO_ONLY), source subnet ranges, min/max ports per VM, idle timeouts (UDP, ICMP, TCP established/transitory/time-wait), endpoint independent mapping, and log config.
google_compute_firewall)Dynamic firewall rules with allow/deny blocks, direction (INGRESS/EGRESS), priority, source/destination ranges, target tags, and target service accounts. Supports multiple protocol/port combinations per rule.
Optional Shared VPC host project enablement via google_compute_shared_vpc_host_project with service project attachment via google_compute_shared_vpc_service_project.
Private DNS managed zones associated with the VPC for internal name resolution. VPC Access Connectors auto-created for subnets with secondary ranges, enabling serverless VPC access.
| Control | Implementation | Details |
|---|---|---|
| Network Segmentation | Custom subnets with specific CIDRs | No auto-create, explicit allocation |
| Firewall Rules | Allow/deny with priority ordering | Target tags or service accounts |
| Private Access | Private Google Access per subnet | Access Google APIs without public IPs |
| Flow Logging | VPC Flow Logs | 5-second intervals, 50% sampling, all metadata |
| NAT Security | Cloud NAT for outbound only | No inbound; configurable port/timeout |
| Default Route Control | Optional default route deletion | Enforce all traffic through NAT/proxy |
| MTU Validation | Range validation 1300-8896 | Terraform validation block |
| Routing Mode Validation | REGIONAL or GLOBAL only | Terraform validation block |
Subnets with secondary ranges for pods/services. Cloud NAT for node egress. Private Google Access for container pulls.
Shared VPC host with service project attachment. Centralized network management with delegated access.
VPC Access Connectors auto-created for subnets. Cloud Functions/Run connect to VPC resources securely.
Global routing mode for cross-region connectivity. Private DNS zones for on-prem integration. Firewall rules for VPN traffic.
| Variable | Type | Default | Description |
|---|---|---|---|
project_id | string | required | GCP project ID |
network_name | string | required | VPC network name |
routing_mode | string | GLOBAL | REGIONAL or GLOBAL (validated) |
mtu | number | 1460 | MTU 1300-8896 (validated) |
subnets | list(object) | [] | Subnet configs with secondary ranges |
firewall_rules | list(object) | [] | Firewall allow/deny rules |
enable_cloud_nat | bool | true | Enable Cloud NAT |
cloud_nat_config | object | {} | NAT IP, ports, timeouts, logging |
enable_shared_vpc_host | bool | false | Enable Shared VPC host |
private_dns_zones | list(object) | [] | Private DNS managed zones |
terraform init terraform plan -out=tfplan terraform apply tfplan # Verify network gcloud compute networks describe $(terraform output -raw network_name) \ --project=YOUR_PROJECT_ID # List subnets gcloud compute networks subnets list \ --network=$(terraform output -raw network_name) \ --project=YOUR_PROJECT_ID