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

Invalid count argument error #82

Open
alvarolorentedev opened this issue Sep 29, 2020 · 36 comments
Open

Invalid count argument error #82

alvarolorentedev opened this issue Sep 29, 2020 · 36 comments
Labels
bug Something isn't working P1 highest priority issues triaged Scoped and ready for work v0.13 Terraform v0.13 issue.

Comments

@alvarolorentedev
Copy link

Currently getting an error when using the module:
Error

Error: Invalid count argument

  on .terraform/modules/gcloud/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.

Module

module "gcloud" {
  source                = "terraform-google-modules/gcloud/google"
  version               = "~> 2.0"
  gcloud_sdk_version    = "304.0.0"
  platform              = "linux"
  additional_components = ["beta"]
  skip_download         = "false"
  count = 1

  create_cmd_triggers = {
    timestamp = local.timestamp
  }

  service_account_key_file = "secrets/terraform-user-credentials.json"

  create_cmd_entrypoint = "scripts/runGcloudCommands.sh"

  depends_on = [module.composer]
}

Environment

  • Terraform version: 0.13.3
  • hashicorp/external: version = "~> 1.2.0"
  • hashicorp/google: version = "~> 3.41.0"
  • hashicorp/google-beta: version = "~> 3.41.0"
  • hashicorp/null: version = "~> 2.1.2"
  • hashicorp/random: version = "~> 2.3.0"
@LangdalP
Copy link

LangdalP commented Oct 7, 2020

I can confirm that I am experiencing the same issue. Searches on the Internet indicates that this started happening to people after upgrading to Terraform version 0.13.x.

@pinglin
Copy link

pinglin commented Oct 9, 2020

I am encountering the same issue as well. Wondering if there is a quick workaround. This has blocked all our regular plan/apply routines.

@davidmontoyago
Copy link

Experiencing the same issue while trying to provision Workload Identities with pre-existing KSAs using terraform-google-kubernetes-engine/tree/v12.0.0/modules/workload-identity.

  • terraform 0.13.4
  • hashicorp/google 3.47.0

@morgante
Copy link
Contributor

Thanks for the reports everyone. So far I've been unable to replicate, but clearly it's an issue. @bharathkkb Have you run into this?

@bharathkkb
Copy link
Member

@morgante yes, I was able to repro from the initial config and TF 0.13.5

module "foo" {
  source                = "terraform-google-modules/gcloud/google"
  version               = "~> 2.0"
  skip_download         = "false"
}

module "bar" {
  source                = "terraform-google-modules/gcloud/google"
  version               = "~> 2.0"
  skip_download         = "false"
  depends_on = [module.foo]
}

@kanekotic could you try using module_depends_on instead?

@davidmontoyago did annotate-sa in the workload-identity module complain? We dont use depends_on in there but if you have an example config to reproduce that would be great!

@morgante morgante added P1 highest priority issues and removed P2 high priority issues labels Nov 13, 2020
@bharathkkb
Copy link
Member

bharathkkb commented Nov 13, 2020

This looks like a side effect of https://www.terraform.io/upgrade-guides/0-13.html#data-resource-reads-can-no-longer-be-disabled-by-refresh-false

In a very simplified config, problem stems from count = data.external.env_override.result.download ? 1 : 0.
When I do tf refresh with a module level depends_on

module.foo.data.external.env_override[0]: Refreshing state...

vs
when I do it without module level depends_on

module.foo.data.external.env_override[0]: Refreshing state... [id=-]
module.bar.data.external.env_override: Refreshing state...

@alvarolorentedev
Copy link
Author

will give it a try and let you know first thing on monday

@davearata-snorack
Copy link

we are able to reproduce without a module level depends_on. Any other workarounds available?

@bharathkkb
Copy link
Member

Hi @davearata-snorack
Could you post a MCVE of this occurring without the depends_on? What version of Terraform?

@zxpower
Copy link

zxpower commented Jan 25, 2021

Tried today with:

  • Terraform v0.14.3
  • hashicorp/google v3.52.0

Issue still persists if depends_on is turned on module level. Any updates on when this will be fixed? 🤔

@MeNsaaH
Copy link

MeNsaaH commented Feb 10, 2021

module "apply-extra-manifests" {
  source  = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
  version = "~> 2.0.2"

  skip_download    = true
  cluster_name     = var.cluster_name
  cluster_location = var.location
  project_id       = var.project_id

  kubectl_create_command  = "kubectl apply ${local.extra_manifests}"
  kubectl_destroy_command = "kubectl delete ${local.extra_manifests}"
}

@bharathkkb this is an example of it not working without the depends_on set on module level. Terraform version 0.13.3

@github-actions
Copy link
Contributor

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@morgante
Copy link
Contributor

morgante commented May 10, 2021

@bharathkkb Do you know if this is still a bug in new Terraform versions?

@morgante morgante added bug Something isn't working triaged Scoped and ready for work labels May 10, 2021
@bharathkkb
Copy link
Member

@morgante this should still be an error with module depends_on as the data source eval will be pushed to apply time. I haven't encountered this with other cases but based on this comment, this is WAI.

Considering we have been steadily reducing the usage of gcloud module we could also consider dropping the env var and requiring plumbing to support this.

@morgante
Copy link
Contributor

Considering we have been steadily reducing the usage of gcloud module we could also consider dropping the env var and requiring plumbing to support this.

I'd like to avoid that, as I still prefer the env variable approach to having to plumb through variables.

We should try to switch to native resources as much as possible though.

@lubouski
Copy link

lubouski commented Jul 16, 2021

Dear google team, faced the. same issue with kubectl-wrapper module
terraform version: Terraform v1.0.2 on darwin_amd64

terraform init                                                                                                                 
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/external from the dependency lock file
- Reusing previous version of hashicorp/google-beta from the dependency lock file
- Reusing previous version of hashicorp/google from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Using previously-installed hashicorp/random v3.1.0
- Using previously-installed hashicorp/external v2.1.0
- Using previously-installed hashicorp/google-beta v3.75.0
- Using previously-installed hashicorp/google v3.75.0
- Using previously-installed hashicorp/null v3.1.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
module "kubectl" {
  source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
  depends_on              = [google_container_cluster.primary]
  project_id              = var.service_google_project
  cluster_name            = google_container_cluster.primary.name
  cluster_location        = google_container_cluster.primary.location
  additional_components   = ["kubectl", "kpt", "beta"]
  kubectl_create_command  = "./scripts/install_asm.sh ${var.service_google_project} ${google_container_cluster.primary.name} ${google_container_cluster.primary.location} asm-downloads"
  kubectl_destroy_command = "./scripts/destroy_asm.sh"
}

Error

terraform plan                                                                                                                 
╷
│ Error: Invalid count argument
│
│   on .terraform/modules/kubectl/main.tf line 57, in resource "random_id" "cache":
│   57:   count = (! local.skip_download) ? 1 : 0
│
#resource "null_resource" "install_asm" {
│ 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

@lubouski Can you try using module_depends_on instead of depends_on as a workaround?

@gtankovics
Copy link

any update regarding this? the problem still occurs in version 1.0.5.

I've tried using as @zxpower. There's a parent module which has depends_on but this module contains only module_depends_on.

@DanielHabenicht
Copy link

@lubouski Can you try using module_depends_on instead of depends_on as a workaround?

This works! Thanks.

@fancybear-dev
Copy link

fancybear-dev commented Oct 19, 2021

Having the same issue here; https://github.com/terraform-google-modules/terraform-google-cloud-operations

Tested with TF version v0.15.3 and v1.0.4.

@fancybear-dev
Copy link

What I did for now was just get all my relating repos offline, change the sources (so they point to the local files, and not remote github) and change the breaking line of code;

In the locals dict, change the skip_download to true, instead of the if statement. That solves it for me. Don't want to lose any more time on this as well, and is fine for now. Note that this is not future proof, as you won't have the latest repo's.

@ghost
Copy link

ghost commented Mar 24, 2022

@lubouski Can you try using module_depends_on instead of depends_on as a workaround?

This worked for me.

djbingham pushed a commit to djbingham/terraform-google-endpoints-dns that referenced this issue Jun 17, 2022
This is a possible workaround proposed by @barathkkb for an the gcloud
module reporting an invalid count argument error.
terraform-google-modules/terraform-google-gcloud#82 (comment)
djbingham pushed a commit to djbingham/terraform-google-endpoints-dns that referenced this issue Jun 22, 2022
This is a possible workaround proposed by @barathkkb for an the gcloud
module reporting an invalid count argument error.
terraform-google-modules/terraform-google-gcloud#82 (comment)
djbingham pushed a commit to djbingham/terraform-google-endpoints-dns that referenced this issue Jun 22, 2022
This is a possible workaround proposed by @barathkkb for an the gcloud
module reporting an invalid count argument error.
terraform-google-modules/terraform-google-gcloud#82 (comment)
@djbingham
Copy link

Considering we have been steadily reducing the usage of gcloud module we could also consider dropping the env var and requiring plumbing to support this.

I'd like to avoid that, as I still prefer the env variable approach to having to plumb through variables.

We should try to switch to native resources as much as possible though.

@morgante Could we rename the GCLOUD_TF_DOWNLOAD variable to something like TF_VAR_GCLOUD_DOWNLOAD?

According to a Terraform support article, Terraform can directly access environment variables with a TF_VAR_ prefix, so that would allow the env_override data source to be removed.

@shawlz
Copy link

shawlz commented Jul 13, 2022

I'm still getting this error on Terraform 1.2.5 and terraform module terraform-google-modules/kubernetes-engine/google v21.2.0 when using ASM

Error: Invalid count argument
on .terraform/modules/asm.cpr/main.tf line 57, in resource "random_id" "cache":
  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.
module "asm" {
  source           = "registry.terraform.io/terraform-google-modules/kubernetes-engine/google//modules/asm"
  version          = "21.2.0"
  project_id       = local.gcp_project_id
  cluster_name     = local.gke_cluster_name
  cluster_location = module.gke.location
  enable_cni       = false

  enable_fleet_registration = true
  enable_mesh_feature       = false

  depends_on = [
    module.gke
  ]
}

Any help would be really appreciated.

@apeabody
Copy link
Contributor

I'm still getting this error on Terraform 1.2.5 and terraform module terraform-google-modules/kubernetes-engine/google v21.2.0 when using ASM

Error: Invalid count argument
on .terraform/modules/asm.cpr/main.tf line 57, in resource "random_id" "cache":
  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.
module "asm" {
  source           = "registry.terraform.io/terraform-google-modules/kubernetes-engine/google//modules/asm"
  version          = "21.2.0"
  project_id       = local.gcp_project_id
  cluster_name     = local.gke_cluster_name
  cluster_location = module.gke.location
  enable_cni       = false

  enable_fleet_registration = true
  enable_mesh_feature       = false

  depends_on = [
    module.gke
  ]
}

Any help would be really appreciated.

Hi @shawlz Could you try replacing depends_on = with module_depends_on =?

@shawlz
Copy link

shawlz commented Jul 13, 2022

@apeabody Yes, here is what I got

Error: Unsupported argument
on anthos.tf line 26, in module "asm":
  module_depends_on = [
An argument named "module_depends_on" is not expected here

@apeabody
Copy link
Contributor

@shawlz Ahh, I see now you are using the asm sub-module. I've open a Feature Request to investigate #121

@shawlz
Copy link

shawlz commented Jul 13, 2022

@shawlz Ahh, I see now you are using the asm sub-module. I've open a Feature Request to investigate #121

Thanks @apeabody

@headegg
Copy link

headegg commented Jul 31, 2022

Hi @apeabody. I've run into the same issue with the workload-identity sub-module. I see the fix that you made to asm under Feature Request #121, adding a module_depends_on var to the module and wiring it in where you use kubectl-wrapper. I tried the same thing in my local workload-identity module and it fixed the problem. Could you please look into applying this to workload-identity?

Am I going about requesting this the right way? Apologies if not. Thanks for a fantastically useful module.

image

@apeabody
Copy link
Contributor

apeabody commented Aug 1, 2022

Hi @headegg - Thanks for reaching out! Typically I would suggest opening a fresh feature request, but not to worry as I've created the pull request for workload-identity which you can track here: terraform-google-modules/terraform-google-kubernetes-engine#1341

@headegg
Copy link

headegg commented Aug 2, 2022

Thanks a million @apeabody. I'll bear that in mind in future.

@aablsk
Copy link

aablsk commented Aug 5, 2022

@apeabody When using module_depends_on on the asm module I get the following error:

╷
│ Error: Invalid value for input variable
│ 
│   on .terraform/modules/asm/modules/asm/main.tf line 66, in module "cpr":
│   66:   module_depends_on = concat([kubernetes_config_map.asm_options], var.module_depends_on)
│ 
│ The given value is not suitable for module.asm.module.cpr.var.module_depends_on declared at .terraform/modules/asm.cpr/modules/kubectl-wrapper/variables.tf:51,1-29: all list elements must have the same type.
╵

This is the corresponding module usage:

module "asm" {
  source                    = "terraform-google-modules/kubernetes-engine/google//modules/asm"
  version                   = "~> 22.1.0"
  project_id                = var.project_id
  cluster_name              = module.gke_development.name
  cluster_location          = module.gke_development.location
  enable_cni                = true
  enable_fleet_registration = true
  enable_mesh_feature       = true

  module_depends_on = [
    module.enabled_google_apis
  ]
}

Thanks for looking into this! 🙏

@apeabody
Copy link
Contributor

apeabody commented Aug 5, 2022

Hi @aablsk - Thanks for reaching out! It's bit more complicated than I expected, but you can track a potential workaround here: terraform-google-modules/terraform-google-kubernetes-engine#1354

@yifan-gu-anchorage
Copy link

yifan-gu-anchorage commented Mar 30, 2023

Hi, the issue still exists when using the asm module ("terraform-google-modules/kubernetes-engine/google//modules/asm")

 Error: Invalid count argument
│
│   on .terraform/modules/anchorage_root.managed_asm_primary.cpr/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.

Could someone take a look into this? Thank you!

Looks like the issue is within these two lines, where the terraform cannot determine the value of the boolean before running the script in data.external.env_override.

@chmstimoteo
Copy link

Same issue here...

Error: Invalid count argument on .terraform/modules/asm.cpr/main.tf line 57, in resource "random_id" "cache": 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.

@chmstimoteo
Copy link

@bharathkkb or @apeabody , a colleague of mine has found a way to prevent this issue from happening when building dependency chains.

Check out this repo: https://github.com/kingman/tf-dont-do-depends-on-module-demo/blob/main/demo-flow/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1 highest priority issues triaged Scoped and ready for work v0.13 Terraform v0.13 issue.
Projects
None yet
Development

No branches or pull requests