A comprehensive Terraform module for creating and managing networking resources on Google Cloud Platform (GCP). This module provides a complete networking setup including VPC networks, subnets, Cloud NAT, firewall rules, routes, and VPC Flow Logs.
- VPC Network: Custom VPC networks with configurable routing modes
- Subnets: Multiple subnets across regions with secondary IP ranges
- Cloud NAT: Optional Cloud NAT with customizable router configuration
- Firewall Rules: Flexible firewall rule definitions with support for INGRESS/EGRESS
- Routes: Custom routing tables with multiple next-hop options
- VPC Flow Logs: Network monitoring with configurable sampling and aggregation
- Modular Design: Clean separation of concerns with individual modules
module "networking" { source = "github.com/your-username/gcp-networking-module" project_id = "my-project" region = "us-central1" network_name = "my-vpc" subnets = [ { name = "subnet-01" ip_cidr_range = "10.10.10.0/24" region = "us-central1" } ] create_nat = true enable_flow_logs = true }
This module creates a fully managed networking infrastructure in GCP with the following components:
- VPC Network: The foundational network container
- Subnets: Regional subnets with optional secondary IP ranges
- Cloud NAT: Provides internet access for private instances
- Firewall Rules: Controls traffic flow based on protocols, ports, and tags
- Routes: Defines network paths and next-hop destinations
- VPC Flow Logs: Monitors and logs network flows for analysis
module "networking" { source = "./modules/networking" project_id = "my-project" region = "us-central1" network_name = "basic-vpc" subnets = [ { name = "web-subnet" ip_cidr_range = "10.0.0.0/24" region = "us-central1" }, { name = "db-subnet" ip_cidr_range = "10.0.1.0/24" region = "us-central1" } ] }
module "networking" { source = "./modules/networking" project_id = "my-project" region = "us-central1" network_name = "advanced-vpc" subnets = [ { name = "prod-subnet" ip_cidr_range = "10.0.0.0/24" region = "us-central1" secondary_ip_ranges = [ { range_name = "pods" ip_cidr_range = "10.1.0.0/16" } ] } ] create_nat = true nat_name = "prod-nat" firewall_rules = [ { name = "allow-ssh" description = "Allow SSH access" direction = "INGRESS" ranges = ["0.0.0.0/0"] allow = [ { protocol = "tcp" ports = ["22"] } ] target_tags = ["ssh"] } ] }
. ├── main.tf # Main configuration ├── variables.tf # Input variables ├── outputs.tf # Output definitions ├── versions.tf # Provider versions ├── modules/ │ ├── vpc/ # VPC and subnet resources │ ├── nat/ # Cloud NAT configuration │ ├── firewall/ # Firewall rule management │ ├── routes/ # Custom route definitions │ └── flow-logs/ # VPC Flow Logs setup └── examples/ └── complete/ # Complete example configuration
Name | Description | Type | Default | Required |
---|---|---|---|---|
project_id | The ID of the GCP project | string | n/a | yes |
region | The region where resources will be created | string | n/a | yes |
network_name | The name of the VPC network | string | n/a | yes |
subnets | List of subnets to be created | list(object) | [] | no |
create_nat | Whether to create a NAT gateway | bool | false | no |
nat_name | The name of the NAT gateway | string | "nat-gateway" | no |
firewall_rules | List of firewall rules to be created | list(object) | [] | no |
routes | List of routes to be created | list(object) | [] | no |
enable_flow_logs | Whether to enable VPC Flow Logs | bool | false | no |
flow_logs_config | Configuration for VPC Flow Logs | object | See variables.tf | no |
Name | Description |
---|---|
network_name | The name of the VPC network |
network_id | The ID of the VPC network |
network_self_link | The self-link of the VPC network |
subnets | The created subnets |
subnet_ids | Map of subnet names to their IDs |
nat_gateway_ip | The external IP address of the NAT gateway |
firewall_rules | The created firewall rules |
routes | The created routes |
- Terraform >= 1.0.0
- Google Provider >= 4.0.0
- Google Beta Provider >= 4.0.0
git clone https://github.com/your-username/gcp-networking-module.git cd gcp-networking-module # Initialize Terraform terraform init # Review the plan terraform plan # Apply the configuration terraform apply
- Subnet Design: Use appropriate CIDR ranges to avoid IP conflicts
- NAT Configuration: Enable NAT for private instances that need internet access
- Firewall Rules: Follow the principle of least privilege
- Flow Logs: Enable for network troubleshooting and security monitoring
- Tagging: Use consistent tags for resource organization
Check the examples/
directory for complete working examples:
examples/complete/
: Full-featured configuration with all components
To run tests:
cd examples/complete terraform init terraform plan
- Thanks to the Terraform community
- Built following GCP best practices
Made with ❤️ for the cloud community