Use the `pre-existing-vpc` module to use an existing Virtual Private Cloud (VPC)
network in Google Cloud and retrieve the network attributes.

This module lets you integrate your pre-provisioned network infrastructure into
new Cluster Toolkit deployments. By using this module, you securely connect new
resources, such as Compute Engine instances, to your established network
environments without the need to recreate the networks. The module outputs
attributes that uniquely identify the VPC network and subnetwork
so that other modules can consume the attributes.

Because the module outputs align with the standard [`vpc`
module](https://docs.cloud.google.com/cluster-toolkit/docs/modules/vpc), you can use the `pre-existing-vpc`
module as a direct substitute.

For the complete list of inputs and outputs for this module, see the
[`pre-existing-vpc`
module](https://github.com/GoogleCloudPlatform/cluster-toolkit/tree/main/modules/network/pre-existing-vpc#inputs)
page in the Cluster Toolkit GitHub repository.

## Before you begin

Before you begin, verify that you meet the following requirements:

- You have installed and configured Cluster Toolkit. For installation instructions, see [Set up
  Cluster Toolkit](https://docs.cloud.google.com/cluster-toolkit/docs/setup/configure-environment).
- You have an existing cluster blueprint. You can use and modify an existing blueprint or create one from scratch. For a working example of a blueprint configured for the `pre-existing-vpc` module, see the [`examples/batch.yaml`](https://github.com/GoogleCloudPlatform/cluster-toolkit/blob/main/examples/batch.yaml) file. For more information about creating and customizing blueprints, see [Cluster blueprint](https://docs.cloud.google.com/cluster-toolkit/docs/setup/cluster-blueprint.md.txt).
- To view a complete list of blueprints, go to the [Cluster blueprint
  catalog](https://docs.cloud.google.com/cluster-toolkit/docs/setup/cluster-blueprint-catalog) page.
- You can use this module to reference a VPC network in a
  different Google Cloud project, such as a Shared VPC host
  project. By default, the module looks up the network and subnetwork in the
  project that is specified by the deployment's `project_id` variable. To
  reference a network in a different project, do the following:

  - Set the `project_id` setting in the `settings` block of the `pre-existing-vpc` module to the ID of the host project.
  - Configure a Cloud NAT gateway and Identity-Aware Proxy (IAP) Transmission Control Protocol (TCP) forwarding. For more information, see [Cloud NAT overview](https://docs.cloud.google.com/nat/docs/overview.md.txt) and [Use IAP for TCP forwarding](https://docs.cloud.google.com/iap/docs/using-tcp-forwarding.md.txt).

### Required roles


To get the permissions that
you need to discover the pre-existing VPC network and subnetwork,

ask your administrator to grant you the
[Compute Network Viewer](https://docs.cloud.google.com/iam/docs/roles-permissions/compute.md.txt#compute.networkViewer) (`roles/compute.networkViewer`) IAM role on the project that contains the VPC network (or
the host project for a Shared VPC network).


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

## Use an existing VPC

The following example identifies a global network and a regional subnetwork. The
`use` keyword instructs the `vm-instance` module to accept the variables that
identify the network and subnetwork so that the cluster creates the compute
instance inside the correct VPC network.

```yaml
- id: network1
  source: modules/network/pre-existing-vpc
  settings:
    project_id: $(vars.project_id)
    region: us-central1

- id: example_vm
  source: modules/compute/vm-instance
  use:
  - network1
  settings:
    name_prefix: example
    machine_type: c2-standard-4
```

The `project_id` setting and the `region` setting are inferred from deployment
variables that share the same name.

### Use with a GKE cluster

When you pair the `pre-existing-vpc` module with the [`gke-cluster`
module](https://docs.cloud.google.com/cluster-toolkit/docs/modules/gke-cluster) to deploy a
Google Kubernetes Engine (GKE) cluster, the existing subnetwork must have
[Private Google Access](https://docs.cloud.google.com/vpc/docs/private-google-access.md.txt) enabled and must
include secondary IP address ranges for pods and services.

Pass the secondary range names to the `gke-cluster` module by using the
`pods_ip_range_name` setting and the `services_ip_range_name` setting, which
default to `pods` and `services`:

```yaml
- id: existing_network
  source: modules/network/pre-existing-vpc
  settings:
    network_name: my-existing-vpc
    subnetwork_name: my-existing-subnet

- id: gke_cluster
  source: modules/scheduler/gke-cluster
  use:
  - existing_network
  settings:
    enable_private_endpoint: false
    pods_ip_range_name: pods
    services_ip_range_name: services
    release_channel: REGULAR
```

> [!NOTE]
> **Note:** The `enable_private_endpoint` setting defaults to `true`. If you run the [`gcluster deploy` command](https://docs.cloud.google.com/cluster-toolkit/docs/reference/gcluster-deploy.md.txt) from outside the VPC network with modules that manage Kubernetes or Helm resources, then set the `enable_private_endpoint` setting to `false`. When you use this configuration, add your IP address range to the allowlist by using the `master_authorized_networks` setting. Alternatively, to run `kubectl` and [`gcluster job`](https://docs.cloud.google.com/cluster-toolkit/docs/reference/gcluster-job.md.txt) commands from outside the network, set the `enable_external_dns_endpoint` setting to `true`.

By default, the `master_ipv4_cidr_block` setting has a `null` value, so
GKE uses Private Service Connect to prevent `/28` CIDR
collisions. For more information about the `master_ipv4_cidr_block` setting, see
[Use a pre-existing VPC
network](https://docs.cloud.google.com/cluster-toolkit/docs/modules/gke-cluster#pre-existing-vpc).

## What's next

- For the complete list of inputs and outputs for this module, see the [`pre-existing-vpc` module](https://github.com/GoogleCloudPlatform/cluster-toolkit/tree/main/modules/network/pre-existing-vpc#inputs) page in the Cluster Toolkit GitHub repository.
- For a complete list of supported modules, see the [compatibility matrix](https://github.com/GoogleCloudPlatform/cluster-toolkit/blob/main/docs/network_storage.md#compatibility-matrix) on GitHub.