'Provision a GCP VM instance with no external IP via Terraform
Trying to create a VM in GCP via terraform with External IP as None.
network_interface {
network = "projects/other-project-name/global/networks/network-name"
subnetwork = "projects/other-project-name/regions/us-central1/subnetworks/subnet-name"
access_config {
nat_ip = "None"
}
}
But nat_ip = "None"
is invalid value for the field. And if I do nat_ip = ""
, it auto assigns External IP.
Here's their documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance#nat_ip
Solution 1:[1]
To create a VM in GCP via terraform without External IP, you can just omit the access_config
section in network_interface
block, as documented here. So you'd have just:
network_interface {
network = "projects/other-project-name/global/networks/network-name"
subnetwork = "projects/other-project-name/regions/us-central1/subnetworks/subnet-name"
}
Solution 2:[2]
Something has changed in the recent updates of the google provider and I couldn't resolve this problem until I've downgraded the network tier to standard, any other combination with network tier PREMIUM (default) or the omission of the block leads to VMs with ephemeral IPs
access_config {
nat_ip = ""
network_tier = "STANDARD"
}
Solution 3:[3]
I confirm the above behavior, as mentioned by LundinCast. In my case, I was not seeing the External IP configured for the VM I was creating using Terraform. I added the blank access_log block under network_interface section, and I got the external ip configured.
network_interface {
network = "default"
access_config {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LundinCast |
Solution 2 | Yeray Álvarez Romero |
Solution 3 | Jai |