Open Alexhha opened 5 years ago
Probably you need nat_ip
, not assigned_nat_ip
?
https://www.terraform.io/docs/providers/google/r/compute_instance.html#network_interface-0-access_config-0-nat_ip
I believe this is a variant of https://github.com/hashicorp/terraform/issues/17048. If so, it could be fixed with a computed convenience field or by 0.12.
I have tried nat_ip
output "gce_public_ip" {
value = "${join("",google_compute_instance.test.network_interface.*.access_config.*.nat_ip)}"
}
But it gives me the same error
Error: Error running plan: 1 error(s) occurred:
* output.gce_public_ip: Resource 'google_compute_instance.gce-test' not found for variable 'google_compute_instance.gce-test.network_interface.*.access_config.*.nat_ip'
If you wanted an ephemeral IP address, you should have used an empty access_config {}
block:
resource "google_compute_instance" "gce-test" {
[...]
network_interface {
subnetwork = "default"
access_config {}
}
[...]
}
Then, for the output:
output "gce_public_ip" {
value = "${google_compute_instance.gce-test.network_interface.0.access_config.0.nat_ip}"
}
It will give an error if resource does not exist.
resource "google_compute_instance" "gce-test1" {
machine_type = "f1-micro"
name = "gce-test1"
count = "${var.gce1_enabled == "true" ? "1": "0" }"
...
}
resource "google_compute_instance" "gce-test2" {
machine_type = "f1-micro"
name = "gce-test2"
count = "${var.gce2_enabled == "true" ? "1": "0" }"
...
}
With the following output
output "gce1-public-ip" {
value = "${google_compute_instance.gce-test1.network_interface.0.access_config.0.nat_ip}"
}
output "gce2-public-ip" {
value = "${google_compute_instance.gce-test2.network_interface.0.access_config.0.nat_ip}"
}
For e.g.
$ terraform apply -var 'gce1_enabled=true' -var 'gce2_enabled=false'
data.google_compute_zones.available: Refreshing state...
Error: Error running plan: 1 error(s) occurred:
* output.gce2-public-ip: Resource 'google_compute_instance.gce-test2' not found for variable 'google_compute_instance.gce-test2.network_interface.0.access_config.0.nat_ip'
and that's the root of the issue
Okay. According to hashicorp/terraform#16726, this output should work:
output "gce_public_ip" {
value = "${element(concat(google_compute_instance.gce-test.*.network_interface.0.access_config.0.nat_ip, list("")), 0)}"
}
I have just tested it and it seems to work.
Yes, works fine. Thanks. But the syntax looks weird
Has this been fixed in 0.12?
In newer versions (e.g. 0.13.5) syntax is changed
Warning: Interpolation-only expressions are deprecated
on main.tf line 45, in output "gce_public_ip":
45: value = "${element(concat(google_compute_instance.vm_instance.*.network_interface.0.access_config.0.nat_ip, list("")), 0)}"
For those who interested following won't show any warning:
output "gce_public_ip" {
value = element(concat(google_compute_instance.vm_instance.*.network_interface.0.access_config.0.nat_ip, list("")), 0)
}
@tillias Also, list("")
can be shortened to [""]
:)
I have no problem with the public ip output as per documentation. I use a blank access_config {} block, then in outputs.tf Don't remember exactly when I started to use this format, could be 0.11.x, then 0.12.x and now still works on 0.14.7, current google provider version is 3.57.0
output "instances_info" {
description = "Compute instance information"
value = [
for instance in google_compute_instance.default :
[instance.network_interface.0.access_config.0.nat_ip]
]
}
In Terraform v1.2.4 worked
output "instances_info" {
description = "Compute instance information"
value = {
for instance in google_compute_instance.instance_name : instance.network_interface.0.access_config.0.nat_ip
}
}
and
output "instances_info" {
description = "Compute instance information"
value = google_compute_instance.instance_name[*].network_interface.0.access_config.0.nat_ip
}
Is this still an issue?
Community Note
Terraform Version
Affected Resource(s)
Terraform Configuration Files
And the output.tf looks like
Debug Output
Panic Output
Expected Behavior
The output should be empty as with google_compute_address resource
Actual Behavior
terraform apply
Error: Error running plan: 1 error(s) occurred:
I have tried a different combinations
but without success. If I replace ephemeral ip address with a static one - everything works fine.
Steps to Reproduce
Important Factoids
References
0000
b/308755570