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

Can't use google compute zones for count iteration like in registry docs #8878

Open
juandiegopalomino opened this issue Apr 9, 2021 · 5 comments

Comments

@juandiegopalomino
Copy link

juandiegopalomino commented Apr 9, 2021

Can't use the google compute zones to iterate through data to fetch even though example

Error: Invalid count argument

  on ../config/tf_modules/gcp-k8s-base/load_balancer.tf line 16, in resource "google_compute_network_endpoint_group" "foo":
  16:   count = length(data.google_compute_zones.zones.names)

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

Tried to use it exactly like in the docs: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_zones

data "google_compute_zones" "available" {
}

resource "google_compute_instance_group_manager" "foo" {
  count = length(data.google_compute_zones.available.names)

  name               = "terraform-test-${count.index}"
  instance_template  = google_compute_instance_template.foobar.self_link
  base_instance_name = "foobar-${count.index}"
  zone               = data.google_compute_zones.available.names[count.index]
  target_size        = 1
}

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 v0.14.10
+ provider registry.terraform.io/hashicorp/google v3.63.0
+ provider registry.terraform.io/hashicorp/helm v2.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/hashicorp/tls v3.1.0

Affected Resource(s)

  • google_compute_zones

Terraform Configuration Files

data "google_compute_zones" "zones" {}

resource "google_compute_network_endpoint_group" "foo" {
  count = length(data.google_compute_zones.zones.names)
  network = data.google_compute_network.vpc.id
  name               = "terraform-test-${count.index}"
  zone               = data.google_compute_zones.zones.names[count.index]
}

Debug Output

Panic Output

Expected Behavior

I can use terraform to loop over resources to create in each zone

Actual Behavior

See original error

Steps to Reproduce

  1. terraform plan

Important Factoids

References

@ghost ghost added the bug label Apr 9, 2021
@venkykuberan venkykuberan self-assigned this Apr 9, 2021
@venkykuberan
Copy link
Contributor

I am able to create compute instances through IGM based out of the zones with the following config. However your error message shows google_compute_network_endpoint_group resource which doesn't have count attribute and its working as expected. If you are trying to use count attribute in IGM, please refer the config below.

resource "google_compute_instance_group_manager" "foo" {
  count = length(data.google_compute_zones.available.names)

  name               = "terraform-test-${count.index}"
  version {
  instance_template  = google_compute_instance_template.instance_template.self_link
  }
  base_instance_name = "foobar-${count.index}"
  zone               = data.google_compute_zones.available.names[count.index]
  target_size        = 2
}

@juandiegopalomino
Copy link
Author

juandiegopalomino commented Apr 9, 2021

Count is a meta argument available to all terraform resources: https://www.terraform.io/docs/language/meta-arguments/count.html

@ghost ghost removed waiting-response labels Apr 9, 2021
@venkykuberan
Copy link
Contributor

I am able to create 4 network end point groups (for the 4 zones) with your config. I am using terraform version 13.x, can you check the same on your end. If its happening only on terraform 14.x then its upstream terraform core issue

Terraform v0.13.4
+ provider registry.terraform.io/hashicorp/google v3.62.0
+ provider registry.terraform.io/hashicorp/google-beta v3.54.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

Plan output

  google_compute_network_endpoint_group.foo[3] will be created
  + resource "google_compute_network_endpoint_group" "foo" {
      + id                    = (known after apply)
      + name                  = "terraform-test-3"
      + network               = "default"
      + network_endpoint_type = "GCE_VM_IP_PORT"
      + project               = (known after apply)
      + self_link             = (known after apply)
      + size                  = (known after apply)
      + zone                  = "us-central1-f"
    }

@juandiegopalomino
Copy link
Author

can you try registry.terraform.io/hashicorp/google v3.63.0 instead of 62?

@ghost ghost removed waiting-response labels Apr 9, 2021
@venkykuberan
Copy link
Contributor

v3.63.0 also works fine, try downgrading your terraform version . If it works then the issue is on TF core

modular-magician added a commit to modular-magician/terraform-provider-google that referenced this issue Sep 22, 2023
* adding security policy field to networkInterfaceAccessConfig

* adding security policy to networkInterface instead of networkInterfaceAccessConfig

* finishing solution 1 and adding integration tests and doc

* cleanups for solution 1

* wrapping update security policy for beta

* replacing the networks in tests

* fixing code review by implementing the solution two

* replacing networks in tests

* fixing nic read for instance_template resource

* adding checking for access config security policy while flattening to prevent the instance template to break

* fixing error while creating instance with empty security_policy

* changing region for tests which use network_edge_security_service

* comment all failing tests but one to test if it runs alone

* making the tests running serially

* fixing the tests to be called only by the serial one

* fixing code review comments

* fixing missing compute_image data from merge

* fixing code review

Signed-off-by: Modular Magician <magic-modules@google.com>
modular-magician added a commit that referenced this issue Sep 22, 2023
* adding security policy field to networkInterfaceAccessConfig

* adding security policy to networkInterface instead of networkInterfaceAccessConfig

* finishing solution 1 and adding integration tests and doc

* cleanups for solution 1

* wrapping update security policy for beta

* replacing the networks in tests

* fixing code review by implementing the solution two

* replacing networks in tests

* fixing nic read for instance_template resource

* adding checking for access config security policy while flattening to prevent the instance template to break

* fixing error while creating instance with empty security_policy

* changing region for tests which use network_edge_security_service

* comment all failing tests but one to test if it runs alone

* making the tests running serially

* fixing the tests to be called only by the serial one

* fixing code review comments

* fixing missing compute_image data from merge

* fixing code review

Signed-off-by: Modular Magician <magic-modules@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants