GCP VM with multiple NICs on separate VPCs
A GCP VM with a configurable number of NICs, each attached to its own VPC network.
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set gcp-ts-vm-multi-niccd pulumi-examples/gcp-ts-vm-multi-nicThis example provisions a Google Compute Engine VM with a configurable number of
network interfaces, where each NIC is attached to its own dedicated VPC network
and subnet. GCE requires that every NIC on a VM lives in a distinct VPC, so this
example creates nicCount VPCs, one subnet per VPC, the matching firewall
rules, and then wires all of them into a single gcp.compute.Instance via a
dynamically-built networkInterfaces list.
Use cases:
- Multi-homed appliances (firewalls, NVAs, routers) that need a foot in several networks.
- Connecting workloads that span a “management” VPC and one or more “data” VPCs.
- Testing inter-VPC routing without VPC peering.
Prerequisites#
- Install Pulumi
- Configure GCP credentials
- Install Node.js
- Access to the
lumitorchPulumi organization and thecloud-creds/gcp-dev-sandboxESC environment (already referenced fromPulumi.yaml).
Deploying the example#
-
Install dependencies:
Terminal window npm install -
Initialize a stack in the
lumitorchorg:Terminal window pulumi stack init lumitorch/devThe stack inherits GCP credentials from the ESC environment
cloud-creds/gcp-dev-sandboxdeclared inPulumi.yaml, so you do not need to setgcp:projector rungcloud authlocally. -
(Optional) Tune the NIC count:
Terminal window pulumi config set nicCount 4pulumi config set machineType n2-standard-8 # n2-standard-8 supports up to 8 NICsGCE caps the number of NICs per VM by machine type — see Multiple network interfaces overview for the matrix.
-
Deploy the stack:
Terminal window pulumi upOutputs include the VM name, zone, and a
nicSummaryarray showing each NIC’s network, subnetwork, internal IP, and (for NIC 0) external IP. -
SSH in via IAP. Only NIC 0 receives a public IP, and the firewall only opens port 22 to the IAP TCP forwarding range:
Terminal window gcloud compute ssh multi-nic-vm --zone $(pulumi stack output vmZone) --tunnel-through-iapOnce inside,
ip -br addrlistsens4,ens5, … — one interface per NIC.
Cleaning up#
Once you’re finished experimenting, destroy your stack and remove it to avoid incurring any additional cost:
pulumi destroypulumi stack rmSummary#
You built a GCE VM with a dynamic number of NICs, each on its own VPC, using Pulumi TypeScript and a Pulumi ESC environment for credentials. From here you can layer routes, add a second VM in any of the secondary VPCs, or swap the hardcoded Debian image for a network-appliance image.