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

Using count with TF 0.13 seems fraught with problems #487

Closed
haf opened this issue Oct 29, 2020 · 13 comments
Closed

Using count with TF 0.13 seems fraught with problems #487

haf opened this issue Oct 29, 2020 · 13 comments

Comments

@haf
Copy link

haf commented Oct 29, 2020

This module crashes when used with TF 0.13 in these cases:

Example when using the budget feature:

Error: Invalid count argument

  on .terraform/modules/host-project/modules/core_project_factory/main.tf line 349, in resource "google_project_usage_export_bucket" "usage_report_export":
 349:   count = var.usage_bucket_name != "" ? 1 : 0

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.

Using a local, so that

-   usage_bucket_name           = google_storage_bucket.usage_report.name
+   usage_bucket_name           = local.usage_report_bucket

makes it pass. Despite this later on in the project factory declaration:

  depends_on = [google_storage_bucket.usage_report]

Moving on, by using the local instead (having to manually declare depends_on as a result of that workaround): later, when using the GSuite integration:

Error: Invalid count argument

  on .terraform/modules/host-project.project-factory.gcloud_disable/main.tf line 57, in resource "random_id" "cache":
  57:   count = (! local.skip_download) ? 1 : 0

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.

This crash seems worse since the only knob I have is a boolean, and that is already "known" statically, so the problem lies inside this module in how it composes its child modules.

Hashicorp has a summary of the count problem:

The use of depends_on is only for ordering in rare circumstances where Terraform is unable to determine dependency orders. For count and for_each, Terraform requires that the arguments are known at time of plan, and you can't use depends_on to delay this until mid-apply.

With how Terraform works at present, we consider this sort of configuration to be incorrect. Using resource computed outputs as arguments to count and for_each is not recommended. Instead, using variables or derived local values which are known at plan time is the preferred approach. If you choose to go ahead with using computed values for count/for_each, this will sometimes require you to work around this using -target as illustrated above.

@morgante
Copy link
Contributor

I'm not sure what you're asking for here.

The limitations of count and for_each are well understood (and based on Terraform core, not this module).

The solution is that you should not use dynamic values as inputs.

@haf
Copy link
Author

haf commented Oct 29, 2020

I'm not, I'm using false, like I wrote above. And it's not me using count, it's this module.

@morgante
Copy link
Contributor

morgante commented Oct 29, 2020

Ok, then where is an error being raised? There isn't an issue with using count, it's an issue with what values are provided to count.

Please share your full Terraform config.

@morgante morgante reopened this Oct 29, 2020
@haf
Copy link
Author

haf commented Oct 29, 2020

locals {
  usage_report_bucket = "example-usage-report"
}

/******************************************
  Provider configuration
 *****************************************/
provider "google" {
  version     = "~> 3.30"
  credentials = file(var.credentials_path)
}

provider "google-beta" {
  version     = "~> 3.30"
  credentials = file(var.credentials_path)
}

provider "null" {
  version = "~> 2.1"
}

provider "random" {
  version = "~> 2.2"
}


/******************************************
  Folders Creation
  https://github.com/terraform-google-modules/terraform-google-project-factory/blob/79f7c953a5267b0d22c2e9396136e27319320ae0/examples/project-hierarchy/main.tf#L54
 *****************************************/

resource "google_folder" "shared" {
  display_name = "Shared"
  parent       = "organizations/${var.organization_id}"
}

resource "google_folder" "labs" {
  display_name = "Labs"
  parent       = "organizations/${var.organization_id}"
}

resource "google_folder" "prod" {
  display_name = "Prod"
  parent       = "organizations/${var.organization_id}"
}


resource "google_storage_bucket" "usage_report" {
  name     = local.usage_report_bucket
  location = "EU"
  project  = var.host_project_name
}

# An additional budget with more options
resource "google_pubsub_topic" "usage_report_alerts" {
  name    = "usage_report_alerts"
  project = var.host_project_name
}
module "host-project" {
  source = "terraform-google-modules/project-factory/google"
  # source  = "terraform-google-modules/project-factory/google//modules/gsuite_enabled"
  version = "~> 9.2"

  activate_apis = [
    "compute.googleapis.com",
    "billingbudgets.googleapis.com"
  ]

  billing_account                = var.billing_account
  bucket_location                = "EU"
  bucket_project                 = "example-seed"
  bucket_name                    = "example-tf-state"
  bucket_versioning              = true
  credentials_path               = var.credentials_path
  default_service_account        = "disable"
  domain                         = "example.com"
  enable_shared_vpc_host_project = true
  folder_id                      = google_folder.shared.id
  lien                           = true
  name                           = var.host_project_name
  org_id                         = var.organization_id
  skip_gcloud_download           = true

  # Budget
  budget_alert_pubsub_topic   = google_pubsub_topic.usage_report_alerts.name
  budget_alert_spent_percents = [0.5, 0.7, 1]
  budget_amount               = 400
  usage_bucket_name           = local.usage_report_bucket

  # GSuite
  # https://github.com/terraform-google-modules/terraform-google-project-factory/issues/484
  # sa_group                       = "gsuite-service-accounts@example.com"
  # create_group                   = true

  depends_on = [google_storage_bucket.usage_report]
}

resource "google_project_iam_member" "project" {
  project = module.host-project.project_id
  role    = "roles/editor"
  member  = "user:example@example.com"
}

@morgante
Copy link
Contributor

What error do you get with this config?

@haf
Copy link
Author

haf commented Oct 29, 2020


Error: Invalid count argument

  on .terraform/modules/host-project.project-factory.gcloud_disable/main.tf line 57, in resource "random_id" "cache":
  57:   count = (! local.skip_download) ? 1 : 0

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.

@morgante
Copy link
Contributor

@haf Could you try leaving the skip_download off entirely?

@morgante
Copy link
Contributor

morgante commented Nov 3, 2020

@EppO This is not the right repo to report issues around Terraform core or the Azure provider.

This is a Google module for Terraform. You want Terraform core: https://github.com/hashicorp/terraform

@morgante morgante closed this as completed Nov 3, 2020
@EppO
Copy link

EppO commented Nov 3, 2020

oh I'm sorry, I ended up here after a google search of the very same issue. I'll open the issue on the terraform repo. Thanks

@haf
Copy link
Author

haf commented Nov 4, 2020

Awesome, it's solved! @morgante How did you solve it?

Could you try leaving the skip_download off entirely?

Works, but is not the solution. It's a bool, it should be possible to set.

@morgante
Copy link
Contributor

morgante commented Nov 4, 2020

@haf Just to confirm, you're no longer seeing this issue?

Works, but is not the solution. It's a bool, it should be possible to set.

Agreed, just trying to diagnose the cause since I can't reproduce.

@haf
Copy link
Author

haf commented Nov 4, 2020

Yes, I think I've worked around it by not using that setting. But have you tried latest Terraform and setting the settings I used? I repro it every time.

@bharathkkb
Copy link
Member

This should now be fixed by #491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants