Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google_compute_instance: ephemeral ip address and splat syntax #3089

Closed
Alexhha opened this issue Feb 20, 2019 · 14 comments
Closed

google_compute_instance: ephemeral ip address and splat syntax #3089

Alexhha opened this issue Feb 20, 2019 · 14 comments

Comments

@Alexhha
Copy link

Alexhha commented Feb 20, 2019

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
  • If an issue is assigned to the "modular-magician" user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to "hashibot", a community member has claimed the issue already.

Terraform Version

$ terraform -v
Terraform v0.11.11
+ provider.google v1.20.0

Affected Resource(s)

  • google_compute_instance

Terraform Configuration Files

resource "google_compute_instance" "gce-test" {
    machine_type = "f1-micro"
    name = "gce-test"
    count = "${var.gce_enabled == "true" ? "1": "0" }"

    boot_disk {
        initialize_params {
            image = "ubuntu-os-cloud/ubuntu-1404-lts"
        }
    }

    network_interface {
        subnetwork = "default"
        access_config {
            nat_ip = ""
        }
    }

    service_account {
        scopes = ["userinfo-email", "compute-ro", "storage-ro"]
    }
}

And the output.tf looks like

output "gce_public_ip" {
    value = "${google_compute_instance.gce-test.network_interface.0.access_config.0.assigned_nat_ip}"
}

Debug Output

Panic Output

Expected Behavior

The output should be empty as with google_compute_address resource

Actual Behavior

  1. terraform apply
$ terraform plan -var gce_enabled=false
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

Error: Error running plan: 1 error(s) occurred:

* output.gce_public_ip: Resource 'google_compute_instance.test' not found for variable 'google_compute_instance.test.network_interface.0.access_config.0.assigned_nat_ip'

I have tried a different combinations

"${google_compute_instance.gce-test.network_interface.*.access_config.0.assigned_nat_ip}"
"${google_compute_instance.gce-test.network_interface.*.access_config.*.assigned_nat_ip}"

but without success. If I replace ephemeral ip address with a static one - everything works fine.

Steps to Reproduce

$ terraform init
$ terraform apply

Important Factoids

References

  • #0000

b/308755570

@ghost ghost added the bug label Feb 20, 2019
@Chupaka
Copy link
Contributor

Chupaka commented Feb 20, 2019

@rileykarson
Copy link
Collaborator

I believe this is a variant of hashicorp/terraform#17048. If so, it could be fixed with a computed convenience field or by 0.12.

@Alexhha
Copy link
Author

Alexhha commented Feb 20, 2019

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'

@MrDanao
Copy link

MrDanao commented Apr 5, 2019

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

@Alexhha
Copy link
Author

Alexhha commented Apr 5, 2019

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

@MrDanao
Copy link

MrDanao commented Apr 5, 2019

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.

@Alexhha
Copy link
Author

Alexhha commented Apr 5, 2019

Yes, works fine. Thanks. But the syntax looks weird

@paddycarver
Copy link
Contributor

Has this been fixed in 0.12?

modular-magician added a commit to modular-magician/terraform-provider-google that referenced this issue Feb 7, 2020
Signed-off-by: Modular Magician <magic-modules@google.com>
modular-magician added a commit that referenced this issue Feb 7, 2020
Signed-off-by: Modular Magician <magic-modules@google.com>
@tillias
Copy link

tillias commented Nov 14, 2020

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)
}

@Chupaka
Copy link
Contributor

Chupaka commented Nov 16, 2020

@tillias Also, list("") can be shortened to [""] :)

@luckeyca
Copy link

luckeyca commented Feb 28, 2021

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

@bro2020
Copy link

bro2020 commented Jul 9, 2022

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
}

@github-actions github-actions bot added forward/review In review; remove label to forward service/compute-instances labels Oct 25, 2023
@edwardmedia edwardmedia removed the forward/review In review; remove label to forward label Oct 30, 2023
@karolgorc
Copy link

Is this still an issue?

Copy link

github-actions bot commented Nov 4, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests