google_compute_subnetwork

A VPC network is a virtual version of the traditional physical networks that exist within and between physical data centers. A VPC network provides connectivity for your Compute Engine virtual machine (VM) instances, Container Engine containers, App Engine Flex services, and other network-related resources.

Each GCP project contains one or more VPC networks. Each VPC network is a global entity spanning all GCP regions. This global VPC network allows VM instances and other resources to communicate with each other via internal, private IP addresses.

Each VPC network is subdivided into subnets, and each subnet is contained within a single region. You can have more than one subnet in a region for a given VPC network. Each subnet has a contiguous private RFC1918 IP space. You create instances, containers, and the like in these subnets. When you create an instance, you must create it in a subnet, and the instance draws its internal IP address from that subnet.

Virtual machine (VM) instances in a VPC network can communicate with instances in all other subnets of the same VPC network, regardless of region, using their RFC1918 private IP addresses. You can isolate portions of the network, even entire subnets, using firewall rules.

To get more information about Subnetwork, see:

Open in Cloud Shell

Example Usage - Subnetwork Basic

resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" {
  name          = "test-subnetwork"
  ip_cidr_range = "10.2.0.0/16"
  region        = "us-central1"
  network       = google_compute_network.custom-test.id
  secondary_ip_range {
    range_name    = "tf-test-secondary-range-update1"
    ip_cidr_range = "192.168.10.0/24"
  }
}

resource "google_compute_network" "custom-test" {
  name                    = "test-network"
  auto_create_subnetworks = false
}
Open in Cloud Shell

Example Usage - Subnetwork Logging Config

resource "google_compute_subnetwork" "subnet-with-logging" {
  name          = "log-test-subnetwork"
  ip_cidr_range = "10.2.0.0/16"
  region        = "us-central1"
  network       = google_compute_network.custom-test.id

  log_config {
    aggregation_interval = "INTERVAL_10_MIN"
    flow_sampling        = 0.5
    metadata             = "INCLUDE_ALL_METADATA"
  }
}

resource "google_compute_network" "custom-test" {
  name                    = "log-test-network"
  auto_create_subnetworks = false
}
Open in Cloud Shell

Example Usage - Subnetwork Internal L7lb

resource "google_compute_subnetwork" "network-for-l7lb" {
  provider = google-beta

  name          = "l7lb-test-subnetwork"
  ip_cidr_range = "10.0.0.0/22"
  region        = "us-central1"
  purpose       = "REGIONAL_MANAGED_PROXY"
  role          = "ACTIVE"
  network       = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
  provider = google-beta

  name                    = "l7lb-test-network"
  auto_create_subnetworks = false
}
Open in Cloud Shell

Example Usage - Subnetwork Ipv6

resource "google_compute_subnetwork" "subnetwork-ipv6" {
  name          = "ipv6-test-subnetwork"

  ip_cidr_range = "10.0.0.0/22"
  region        = "us-west2"

  stack_type       = "IPV4_IPV6"
  ipv6_access_type = "EXTERNAL"

  network       = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
  name                    = "ipv6-test-network"
  auto_create_subnetworks = false
}
Open in Cloud Shell

Example Usage - Subnetwork Internal Ipv6

resource "google_compute_subnetwork" "subnetwork-internal-ipv6" {
  name          = "internal-ipv6-test-subnetwork"

  ip_cidr_range = "10.0.0.0/22"
  region        = "us-west2"

  stack_type       = "IPV4_IPV6"
  ipv6_access_type = "INTERNAL"

  network       = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
  name                    = "internal-ipv6-test-network"
  auto_create_subnetworks = false
  enable_ula_internal_ipv6 = true
}
Open in Cloud Shell

Example Usage - Subnetwork Purpose Private Nat

resource "google_compute_subnetwork" "subnetwork-purpose-private-nat" {
  provider         = google-beta

  name             = "subnet-purpose-test-subnetwork"
  region           = "us-west2"
  ip_cidr_range    = "192.168.1.0/24"
  purpose          = "PRIVATE_NAT"
  network          = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
  provider                = google-beta

  name                    = "subnet-purpose-test-network"
  auto_create_subnetworks = false
}
Open in Cloud Shell

Example Usage - Subnetwork Cidr Overlap

resource "google_compute_subnetwork" "subnetwork-cidr-overlap" {
  provider = google-beta

  name                             = "subnet-cidr-overlap"
  region                           = "us-west2"
  ip_cidr_range                    = "192.168.1.0/24"
  allow_subnet_cidr_routes_overlap = true
  network                          = google_compute_network.net-cidr-overlap.id
}

resource "google_compute_network" "net-cidr-overlap" {
  provider                = google-beta

  name                    = "net-cidr-overlap"
  auto_create_subnetworks = false
}

Argument Reference

The following arguments are supported:


The secondary_ip_range block supports:

The log_config block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

This resource provides the following Timeouts configuration options:

Import

Subnetwork can be imported using any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import Subnetwork using one of the formats above. For example:

import {
  id = "projects/{{project}}/regions/{{region}}/subnetworks/{{name}}"
  to = google_compute_subnetwork.default
}

When using the terraform import command, Subnetwork can be imported using one of the formats above. For example:

$ terraform import google_compute_subnetwork.default projects/{{project}}/regions/{{region}}/subnetworks/{{name}}
$ terraform import google_compute_subnetwork.default {{project}}/{{region}}/{{name}}
$ terraform import google_compute_subnetwork.default {{region}}/{{name}}
$ terraform import google_compute_subnetwork.default {{name}}

User Project Overrides

This resource supports User Project Overrides.