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"]
}
}
An empty access_config block would assign an external ephemeral IP to your instance.
network_interface {
network = "default"
access_config {}
}
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 :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.