9

I need to create a VM instance in gcp with a public IP (instance can randomly pick one for itself) without explicitly defining a one.

So how can I do this?

This is the gcloud command that I can use to achieve this (create a vm instance with automatically assigned public ip)

gcloud compute instances create controller-1 \
    --async \
    --boot-disk-size 200GB \
    --can-ip-forward \
    --image-family ubuntu-2004-lts \
    --image-project ubuntu-os-cloud \
    --machine-type e2-standard-2 \
    --private-network-ip 10.240.0.10 \
    --scopes compute-rw,storage-ro,service-management,service-control,logging-write,monitoring \
    --subnet kubernetes \
    --tags kubernetes-the-hard-way,controller

Above command will create a vm with both internal IP 10.240.0.10 and a Public ip with some randomly picked ip address.

So I want to achieve the same goal with terraform

This is my terraform code. but how can I do this?

resource "google_compute_instance" "controllers" {
  name         = "controller-0"
  machine_type = "e2-standard-2"
  zone         = var.zone

  can_ip_forward = true



  tags = ["kubernetes-the-hard-way", "controller"]
  

  boot_disk {
    initialize_params {
      image = "ubuntu-2004-focal-v20200720"
    }
  }

  network_interface {
    subnetwork = google_compute_subnetwork.kubernetes.name
    network_ip = "10.240.0.10"  // private ip but how to assign a public ip (randomly)  
  }

  service_account {
    scopes = ["compute-rw", "storage-ro", "service-management", "service-control", "logging-write", "monitoring"]
  }
}

2 Answers 2

17

An empty access_config block would assign an external ephemeral IP to your instance.

network_interface {
    network = "default"
    access_config {}
}
Sign up to request clarification or add additional context in comments.

Comments

8

Looks like you need to specify "access_config" under "network_interface" to assign external(public) IP to GCE instance according to this example from terraform.

resource "google_compute_address" "static" {
  name = "ipv4-address"
}

data "google_compute_image" "debian_image" {
  family  = "debian-9"
  project = "debian-cloud"
}

resource "google_compute_instance" "instance_with_ip" {
  name         = "vm-instance"
  machine_type = "f1-micro"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = data.google_compute_image.debian_image.self_link
    }
  }

  network_interface {
    network = "default"
    access_config {
      nat_ip = google_compute_address.static.address
    }
  }
}

Google Cloud Platform's Compute Engine Supports two types of external IP addresses:

Static external IP addresses

Ephemeral external IP addresses

3 Comments

... a screenshot?
It's worth noting that you do pay for a static IP address, around $7.20 a month last I checked. The ephemeral option is free.
How to assign nat_ip already reserved IP by the name of that address resource in GCP?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.