hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.34k stars 1.74k forks source link

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

Open juandiegopalomino opened 3 years ago

juandiegopalomino commented 3 years ago

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

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)

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

venkykuberan commented 3 years ago

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 commented 3 years ago

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

venkykuberan commented 3 years ago

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 commented 3 years ago

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

venkykuberan commented 3 years ago

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