Google Compute Engine VPN Tunnel

This page shows how to write Terraform for Compute Engine VPN Tunnel and write them securely.

google_compute_vpn_tunnel (Terraform)

The VPN Tunnel in Compute Engine can be configured in Terraform with the resource name google_compute_vpn_tunnel. The following sections describe 2 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L42
resource "google_compute_vpn_tunnel" "tunnel1" {
  name                  = "gcve-to-filestore-tunnel1"
  region                = var.region
  vpn_gateway           = google_compute_ha_vpn_gateway.gcve_gateway.id
  peer_gcp_gateway      = google_compute_ha_vpn_gateway.filestore_gateway.id
  shared_secret         = var.vpn_psk
main.tf#L55
resource "google_compute_vpn_tunnel" "tunnel1" {
  provider         = "google-beta"
  project     = var.gcp_project_id
  name             = var.tunnel_name_if1
  region           = var.gcp_region
  vpn_gateway      = var.ha_vpn_gateway

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Creation timestamp in RFC3339 text format.

An optional description of this resource.

Detailed status message for the VPN tunnel.

IKE protocol version to use when establishing the VPN tunnel with peer VPN gateway. Acceptable IKE versions are 1 or 2. Default version is 2.

Local traffic selector to use when establishing the VPN tunnel with peer VPN gateway. The value should be a CIDR formatted string, for example '192.168.0.0/16'. The ranges should be disjoint. Only IPv4 is supported.

Name of 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.

URL of the peer side external VPN gateway to which this VPN tunnel is connected.

The interface ID of the external VPN gateway to which this VPN tunnel is connected.

URL of the peer side HA GCP VPN gateway to which this VPN tunnel is connected. If provided, the VPN tunnel will automatically use the same vpn_gateway_interface ID in the peer GCP VPN gateway. This field must reference a 'google_compute_ha_vpn_gateway' resource.

IP address of the peer VPN gateway. Only IPv4 is supported.

The region where the tunnel is located. If unset, is set to the region of 'target_vpn_gateway'.

Remote traffic selector to use when establishing the VPN tunnel with peer VPN gateway. The value should be a CIDR formatted string, for example '192.168.0.0/16'. The ranges should be disjoint. Only IPv4 is supported.

URL of router resource to be used for dynamic routing.

Shared secret used to set the secure session between the Cloud VPN gateway and the peer VPN gateway.

Hash of the shared secret.

URL of the Target VPN gateway with which this VPN tunnel is associated.

The unique identifier for the resource. This identifier is defined by the server.

URL of the VPN gateway with which this VPN tunnel is associated. This must be used if a High Availability VPN gateway resource is created. This field must reference a 'google_compute_ha_vpn_gateway' resource.

The interface ID of the VPN gateway with which this VPN tunnel is associated.

Explanation in Terraform Registry

VPN tunnel resource. To get more information about VpnTunnel, 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.

risk-label

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.

risk-label

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.

risk-label

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.

risk-label

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.

risk-label

google_compute_ssl_policy

Ensure to use modern TLS protocols

It's better to adopt TLS v1.2+ instead of outdated TLS protocols.

risk-label

google_compute_subnetwork

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.

Review your Google Compute Engine settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Frequently asked questions

What is Google Compute Engine VPN Tunnel?

Google Compute Engine VPN Tunnel 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 VPN Tunnel?

For Terraform, the PacketBeta/gcve-cloudvpn-transit-vpc and tranquilitybase-io/tb-module-gcp-aws-vpn source code examples are useful. See the Terraform Example section for further details.

security-icon

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.