» 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:

» Example Usage - Subnetwork Basic

resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" {
  name          = "test-subnetwork"
  ip_cidr_range = ""
  region        = "us-central1"
  network       = "${google_compute_network.custom-test.self_link}"
  secondary_ip_range {
    range_name    = "tf-test-secondary-range-update1"
    ip_cidr_range = ""

resource "google_compute_network" "custom-test" {
  name                    = "test-network"
  auto_create_subnetworks = false

» Example Usage - Subnetwork Logging Config Beta

resource "google_compute_subnetwork" "subnet-with-logging" {
  provider      = "google-beta" 
  name          = "log-test-subnetwork"
  ip_cidr_range = ""
  region        = "us-central1"
  network       = "${google_compute_network.custom-test.self_link}"

  enable_flow_logs = true
  log_config {
    aggregation_interval = "INTERVAL_10_MIN"
    flow_sampling = 0.5
    metadata = "INCLUDE_ALL_METADATA"

resource "google_compute_network" "custom-test" {
  provider                = "google-beta"
  name                    = "log-test-network"
  auto_create_subnetworks = false

provider "google-beta"{
  region = "us-central1"
  zone   = "us-central1-a"

» Argument Reference

The following arguments are supported:

  • ip_cidr_range - (Required) The range of internal addresses that are owned by this subnetwork. Provide this property when you create the subnetwork. For example, or Ranges must be unique and non-overlapping within a network. Only IPv4 is supported.

  • name - (Required) The name of the resource, provided by the client when initially creating the resource. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression [a-z]([-a-z0-9]*[a-z0-9])? which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.

  • network - (Required) The network this subnet belongs to. Only networks that are in the distributed mode can have subnetworks.

  • description - (Optional) An optional description of this resource. Provide this property when you create the resource. This field can be set only at resource creation time.

  • enable_flow_logs - (Optional) Whether to enable flow logging for this subnetwork.

  • secondary_ip_range - (Optional) An array of configurations for secondary IP ranges for VM instances contained in this subnetwork. The primary IP of such VM must belong to the primary ipCidrRange of the subnetwork. The alias IPs may belong to either primary or secondary ranges. Structure is documented below.

  • private_ip_google_access - (Optional) When enabled, VMs in this subnetwork without external IP addresses can access Google APIs and services by using Private Google Access.

  • region - (Optional) URL of the GCP region for this subnetwork.

  • log_config - (Optional, Beta) Denotes the logging options for the subnetwork flow logs. If logging is enabled logs will be exported to Stackdriver. Structure is documented below.

  • project - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

The secondary_ip_range block supports:

  • range_name - (Required) The name associated with this subnetwork secondary range, used when adding an alias IP range to a VM instance. The name must be 1-63 characters long, and comply with RFC1035. The name must be unique within the subnetwork.

  • ip_cidr_range - (Required) The range of IP addresses belonging to this subnetwork secondary range. Provide this property when you create the subnetwork. Ranges must be unique and non-overlapping with all primary and secondary IP ranges within a network. Only IPv4 is supported.

The log_config block supports:

  • aggregation_interval - (Optional) Can only be specified if VPC flow logging for this subnetwork is enabled. Toggles the aggregation interval for collecting flow logs. Increasing the interval time will reduce the amount of generated flow logs for long lasting connections. Default is an interval of 5 seconds per connection.

  • flow_sampling - (Optional) Can only be specified if VPC flow logging for this subnetwork is enabled. The value of the field must be in [0, 1]. Set the sampling rate of VPC flow logs within the subnetwork where 1.0 means all collected logs are reported and 0.0 means no logs are reported. Default is 0.5 which means half of all collected logs are reported.

  • metadata - (Optional) Can only be specified if VPC flow logging for this subnetwork is enabled. Configures whether metadata fields should be added to the reported VPC flow logs. Default is INCLUDE_ALL_METADATA.

» Attributes Reference

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

  • creation_timestamp - Creation timestamp in RFC3339 text format.

  • gateway_address - The gateway address for default routes to reach destination addresses outside this subnetwork.

  • fingerprint - Fingerprint of this resource. This field is used internally during updates of this resource.

  • self_link - The URI of the created resource.

» Timeouts

This resource provides the following Timeouts configuration options:

  • create - Default is 6 minutes.
  • update - Default is 6 minutes.
  • delete - Default is 6 minutes.

» Import

Subnetwork can be imported using any of these accepted formats:

$ 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}}