terraform-provider-kubectl: The "count" value depends on resource attributes that cannot be determined until apply

Hello!

I have the following Terraform code inside of a module:

data "kubectl_path_documents" "esCustomResourcesManifests" {
  pattern = "modules/elasticsearch/all-in-one-1.3.0.yaml"
  disable_template = true
}

resource "kubectl_manifest" "esCustomResources" {
  count     = length(data.kubectl_path_documents.esCustomResourcesManifests.documents)
  yaml_body = element(data.kubectl_path_documents.esCustomResourcesManifests.documents, count.index)
}

When running terraform plan, I get:

Error: Invalid count argument

  on modules/elasticsearch/elasticSearch.tf line 24, in resource "kubectl_manifest" "esCustomResources":
  24:   count     = length(data.kubectl_path_documents.esCustomResourcesManifests.documents)

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 does not seem to be the same as issue #58, since I am not pulling any external variables. Is there something obvious I am missing?

Versions: Terraform v0.13.5 provider registry.terraform.io/gavinbunney/kubectl v1.9.1

YAML file: https://download.elastic.co/downloads/eck/1.3.0/all-in-one.yaml

Thanks in advance for your help!

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 27
  • Comments: 43 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I managed to build a hacky workaround that might work for most of you guys… Basically I replaced the count source using other terraform’s built-in functions to fetch the document count…

Here’s an example (taken from by previous comment:

data "kubectl_path_documents" "secrets_test" {
  pattern = "${path.module}/k8s/secrets-test/*.yaml"
}

resource "kubectl_manifest" "secrets_test" {
  count = length(
    flatten(
      toset([
        for f in fileset(".", data.kubectl_path_documents.secrets_test.pattern) : split("\n---\n", file(f))
        ]
      )
    )
  )
  yaml_body = element(data.kubectl_path_documents.secrets_test.documents, count.index)
}

What is happening here:

  • I calculate all the possible documents in the same path that the data block is using using a fileset function
  • I wrap that result in a for loop and to support the yaml’s multi-document feature, I’m also splitting each file with \n---\n to ensure that I only grab the correct ones.
  • In the end I must flatten the set because in case if multi-documents, there will be arrays inside arrays.

I know this is not an elegant solution and there might be other issues that I missed, but it’s better than using -target to get around the issue…

I tested locally with multiple scenarios and for now this workaround works for me.

Any updates on this issue ? Am facing similar problem, this happens only if it is used inside a module

this happens only if it is used inside a module

@gavinbunney Confirm, in my case it does not work inside a module

  • exact same behaviour when I swap kubectl_path_documents to kubectl_filename_list

@gavinbunney are we doing something wrong? It seems to me like I really took the configration straight out of the documentation.

On top of that, it worked but now, with a clean state, terraform does not work because of that count…

I’ve had clean, multiple runs from 2 different modules and then suddenly got this error on the one module. Using 1.14.0 of the kubectl provider and 1.2.9 for TF.