Google Compute Engine Subnetwork
This page shows how to write Terraform for Compute Engine Subnetwork and write them securely.
google_compute_subnetwork (Terraform)
The Subnetwork in Compute Engine can be configured in Terraform with the resource name google_compute_subnetwork
. The following sections describe 3 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "google_compute_subnetwork" "private_subnet1a" {
name = format("private-subnet-%sa", var.project_region)
ip_cidr_range = var.private_subnet_1a
network = google_compute_network.vpc_network.self_link
region = var.project_region
}
resource "google_compute_subnetwork" "eu_deployment" {
name = "eu-production-subnet"
ip_cidr_range = "10.1.0.0/16"
region = "europe-west4"
network = google_compute_network.vpc_idorg_prod.id
}
resource "google_compute_subnetwork" "mtl-dmz" {
name = "mtl-dmz"
ip_cidr_range = "172.16.1.0/24"
region = "us-east1"
network = google_compute_network.cr460demo.self_link
}
Security Best Practices for google_compute_subnetwork
There is 1 setting in google_compute_subnetwork that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure VPC flow logging is enabled
It is better to enable VPC flow logging. VPC flow logging allows us to audit traffic in your network.
Parameters
-
creation_timestamp
optional computed - string
Creation timestamp in RFC3339 text format.
-
description
optional - string
An optional description of this resource. Provide this property when you create the resource. This field can be set only at resource creation time.
-
fingerprint
optional computed - string
Fingerprint of this resource. This field is used internally during updates of this resource.
-
gateway_address
optional computed - string
The gateway address for default routes to reach destination addresses outside this subnetwork.
-
id
optional computed - string -
ip_cidr_range
required - string
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 - string
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?' 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 - string
The network this subnet belongs to. Only networks that are in the distributed mode can have subnetworks.
-
private_ip_google_access
optional - bool
When enabled, VMs in this subnetwork without external IP addresses can access Google APIs and services by using Private Google Access.
-
private_ipv6_google_access
optional computed - string
The private IPv6 google access type for the VMs in this subnet.
The GCP region for this subnetwork.
-
secondary_ip_range
optional computed - list of object
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.
-
ip_cidr_range
- string -
range_name
- string -
self_link
optional computed - string -
log_config
list block-
aggregation_interval
optional - string
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: "INTERVAL_5_SEC" Possible values: ["INTERVAL_5_SEC", "INTERVAL_30_SEC", "INTERVAL_1_MIN", "INTERVAL_5_MIN", "INTERVAL_10_MIN", "INTERVAL_15_MIN"]
-
filter_expr
optional - string
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.
-
flow_sampling
optional - number
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 - string
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: "INCLUDE_ALL_METADATA" Possible values: ["EXCLUDE_ALL_METADATA", "INCLUDE_ALL_METADATA", "CUSTOM_METADATA"]
-
metadata_fields
optional - set of string
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.
-
-
timeouts
single block
Explanation in Terraform Registry
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:
Tips: Best Practices for The Other Google Compute Engine Resources
In addition to the google_compute_disk, Google Compute Engine has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
google_compute_disk
Ensure the encryption key for your GCE disk is stored securely
It is better to store the encryption key for your GCE disk securely. Secret Manager could be used instead.
google_compute_firewall
Ensure your VPC firewall blocks unwanted outbound traffic
It is better to block unwanted outbound traffic not to expose resources in the VPC to unwanted attacks.
google_compute_instance
Ensure appropriate service account is assigned to your GCE instance
It is better to create a custom service account for the instance and assign it.
google_compute_project_metadata
Ensure OS login for your GCE instances is enabled at project level
It is better to enable OS login for your GCE instances. Enabling OS login ensures that SSH keys used to connect to instances are mapped with IAM users, allowing centralized and automated SSH key management.
google_compute_ssl_policy
Ensure to use modern TLS protocols
It's better to adopt TLS v1.2+ instead of outdated TLS protocols.
Frequently asked questions
What is Google Compute Engine Subnetwork?
Google Compute Engine Subnetwork is a resource for Compute Engine of Google Cloud Platform. Settings can be wrote in Terraform.
Where can I find the example code for the Google Compute Engine Subnetwork?
For Terraform, the msfidelis/kubernetes-with-cri-o, mbdebian/terraform-playground and kaks2015/Terraform4-CR460-H2021 source code examples are useful. See the Terraform Example section for further details.