Skip to content

googleComputeSubnetwork

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

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeNetworkCustomTest = new google.computeNetwork.ComputeNetwork(
  this,
  "custom-test",
  {
    auto_create_subnetworks: false,
    name: "test-network",
  }
);
new google.computeSubnetwork.ComputeSubnetwork(
  this,
  "network-with-private-secondary-ip-ranges",
  {
    ip_cidr_range: "10.2.0.0/16",
    name: "test-subnetwork",
    network: googleComputeNetworkCustomTest.id,
    region: "us-central1",
    secondary_ip_range: [
      {
        ip_cidr_range: "192.168.10.0/24",
        range_name: "tf-test-secondary-range-update1",
      },
    ],
  }
);

Example Usage - Subnetwork Logging Config

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeNetworkCustomTest = new google.computeNetwork.ComputeNetwork(
  this,
  "custom-test",
  {
    auto_create_subnetworks: false,
    name: "log-test-network",
  }
);
new google.computeSubnetwork.ComputeSubnetwork(this, "subnet-with-logging", {
  ip_cidr_range: "10.2.0.0/16",
  log_config: [
    {
      aggregation_interval: "INTERVAL_10_MIN",
      flow_sampling: 0.5,
      metadata: "INCLUDE_ALL_METADATA",
    },
  ],
  name: "log-test-subnetwork",
  network: googleComputeNetworkCustomTest.id,
  region: "us-central1",
});

Example Usage - Subnetwork Internal L7lb

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeNetworkCustomTest = new google.computeNetwork.ComputeNetwork(
  this,
  "custom-test",
  {
    auto_create_subnetworks: false,
    name: "l7lb-test-network",
    provider: "${google-beta}",
  }
);
new google.computeSubnetwork.ComputeSubnetwork(this, "network-for-l7lb", {
  ip_cidr_range: "10.0.0.0/22",
  name: "l7lb-test-subnetwork",
  network: googleComputeNetworkCustomTest.id,
  provider: "${google-beta}",
  purpose: "INTERNAL_HTTPS_LOAD_BALANCER",
  region: "us-central1",
  role: "ACTIVE",
});

Example Usage - Subnetwork Ipv6

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeNetworkCustomTest = new google.computeNetwork.ComputeNetwork(
  this,
  "custom-test",
  {
    auto_create_subnetworks: false,
    name: "ipv6-test-network",
  }
);
new google.computeSubnetwork.ComputeSubnetwork(this, "subnetwork-ipv6", {
  ip_cidr_range: "10.0.0.0/22",
  ipv6_access_type: "EXTERNAL",
  name: "ipv6-test-subnetwork",
  network: googleComputeNetworkCustomTest.id,
  region: "us-west2",
  stack_type: "IPV4_IPV6",
});

Example Usage - Subnetwork Internal Ipv6

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
const googleComputeNetworkCustomTest = new google.computeNetwork.ComputeNetwork(
  this,
  "custom-test",
  {
    auto_create_subnetworks: false,
    enable_ula_internal_ipv6: true,
    name: "internal-ipv6-test-network",
  }
);
new google.computeSubnetwork.ComputeSubnetwork(
  this,
  "subnetwork-internal-ipv6",
  {
    ip_cidr_range: "10.0.0.0/22",
    ipv6_access_type: "INTERNAL",
    name: "internal-ipv6-test-subnetwork",
    network: googleComputeNetworkCustomTest.id,
    region: "us-west2",
    stack_type: "IPV4_IPV6",
  }
);

Argument Reference

The following arguments are supported:

  • ipCidrRange - (Required) The range of internal addresses that are owned by this subnetwork. Provide this property when you create the subnetwork. For example, 10.0.0.0/8 or 192.168.0.0/16. 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 [aZ]([AZ09]*[aZ09])? 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.

  • purpose - (Optional) The purpose of the resource. This field can be either privateRfc1918, internalHttpsLoadBalancer or regionalManagedProxy. A subnetwork with purpose set to internalHttpsLoadBalancer is a user-created subnetwork that is reserved for Internal HTTP(S) Load Balancing. A subnetwork in a given region with purpose set to regionalManagedProxy is a proxy-only subnet and is shared between all the regional Envoy-based load balancers. If unspecified, the purpose defaults to privateRfc1918. The enableFlowLogs field isn't supported with the purpose field set to internalHttpsLoadBalancer.

  • role - (Optional) The role of subnetwork. The value can be set to active or backup. An active subnetwork is one that is currently being used. A backup subnetwork is one that is ready to be promoted to active or is currently draining. Subnetwork role must be specified when purpose is set to internalHttpsLoadBalancer or regionalManagedProxy. Possible values are active and backup.

  • secondaryIpRange - (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. Note: This field uses attr-as-block mode to avoid breaking users during the 0.12 upgrade. To explicitly send a list of zero objects you must use the following syntax: example=[] For more details about this behavior, see this section. Structure is documented below.

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

  • privateIpv6GoogleAccess - (Optional) The private IPv6 google access type for the VMs in this subnet.

  • region - (Optional) The GCP region for this subnetwork.

  • logConfig - (Optional) Denotes the logging options for the subnetwork flow logs. If logging is enabled logs will be exported to Stackdriver. This field cannot be set if the purpose of this subnetwork is internalHttpsLoadBalancer Structure is documented below.

  • stackType - (Optional) The stack type for this subnet to identify whether the IPv6 feature is enabled or not. If not specified IPV4_ONLY will be used. Possible values are ipv4Only and ipv4Ipv6.

  • ipv6AccessType - (Optional) The access type of IPv6 address this subnet holds. It's immutable and can only be specified during creation or the first time the subnet is updated into IPV4_IPV6 dual stack. If the ipv6_type is EXTERNAL then this subnet cannot enable direct path. Possible values are external and internal.

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

The secondaryIpRange block supports:

  • rangeName - (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.

  • ipCidrRange - (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 logConfig block supports:

  • aggregationInterval - (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. Default value is interval5Sec. Possible values are interval5Sec, interval30Sec, interval1Min, interval5Min, interval10Min, and interval15Min.

  • flowSampling - (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 value is includeAllMetadata. Possible values are excludeAllMetadata, includeAllMetadata, and customMetadata.

  • metadataFields - (Optional) List of metadata fields that should be added to reported logs. Can only be specified if VPC flow logs for this subnetwork is enabled and "metadata" is set to CUSTOM_METADATA.

  • filterExpr - (Optional) Export filter used to define which VPC flow logs should be logged, as as CEL expression. See https://cloud.google.com/vpc/docs/flow-logs#filtering for details on how to format this field. The default value is 'true', which evaluates to include everything.

Attributes Reference

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

  • id - an identifier for the resource with format projects/{{project}}/regions/{{region}}/subnetworks/{{name}}

  • creationTimestamp - Creation timestamp in RFC3339 text format.

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

  • ipv6CidrRange - The range of internal IPv6 addresses that are owned by this subnetwork.

  • externalIpv6Prefix - The range of external IPv6 addresses that are owned by this subnetwork.

  • selfLink - The URI of the created resource.

Timeouts

This resource provides the following Timeouts configuration options:

  • create - Default is 20 minutes.
  • update - Default is 20 minutes.
  • delete - Default is 20 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}}

User Project Overrides

This resource supports User Project Overrides.