Count inside modules bug

I was using kubectl provider and came across a weird bug which I’m pretty sure is terraform not the provider. The following file is inside a module:

data "kubectl_file_documents" "xray_daemon" {
    content = templatefile("${path.module}/xray-templ.yaml",
      namespace = var.namespace
      role_name =
      account_id = data.aws_caller_identity.current.account_id

resource "kubectl_manifest" "manifest" {
  count     = length(data.kubectl_file_documents.xray_daemon_doc_count.documents)
  yaml_body = data.kubectl_file_documents.xray_daemon.documents[count.index]

terraform apply complains that it cannot compute length():

Error: Invalid count argument
│   on /deployment-tool/templates/cluster-config/schema-2.1/creation-module/aws-xray/ line 43, in resource "kubectl_manifest" "manifest":
│   43:   count     = length(data.kubectl_file_documents.xray_daemon.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.

If I move this file outside of the module, and adjust the path, it works fine! This is observed by several people at The "count" value depends on resource attributes that cannot be determined · Issue #58 · gavinbunney/terraform-provider-kubectl · GitHub. In my example, the only “external” dependency is var.namespace. If I remove it, the file works inside the module too.

Any sense of what is happening here, and if this is a bug in terraform, or a limitation of the provider, or correct behavior? I doubt correct behavior because there should be no difference in computation whether data source is in module or outside.

We’ve seen this as well, but found it only happened to us within a module which we had a depends_on IE: implicit dependency listed on the module.

Once we either:
A) moved this code to the calling terraform, or
B) modified the module call to not have the implicit dependency

it would work.
Note there didn’t seem to be any issues with non-implicit dependencies IE: you just had a variable which came from another module.

1 Like