Search code examples
google-cloud-platformgoogle-compute-engineterraform

How do I attach a public AND private reserved IP to a GCE instance?


https://www.terraform.io/docs/providers/google/r/compute_instance.html

I want to reserve a external and internal IP but how do I attach both? I don't see examples in the TF docs.

It only has network_ip - (Optional) The *private* IP address ... https://www.terraform.io/docs/providers/google/r/compute_instance.html#network_interface


Solution

  • This is a working example within my compute instance module:

    resource "google_compute_address" "internal" {
      name         = "${var.NAME}-int-ip"
      subnetwork   = "${var.SUBNETWORK}"
      address_type = "INTERNAL"
      address      = "${var.PRIVATE_IP}"
      region       = "${var.REGION}"
    }
    
    resource "google_compute_address" "external" {
      name         = "${var.NAME}-ext-ip"
      address_type = "EXTERNAL"
      region       = "${var.REGION}"
    }
    

    And then within the google_compute_instance resource block set the IP within the network_infrastructure block:

    network_interface {
       network= "${var.NETWORK}"
       network_ip = "${google_compute_address.internal.address}"
       access_config {
          nat_ip = "${google_compute_address.external.address}" 
       }