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"]
}
}