# Count inside modules bug

**URL:** <https://discuss.hashicorp.com/t/count-inside-modules-bug/27553>\
**Category:** Terraform\
**Created:** [July 30, 2021, 9:03am UTC](https://discuss.hashicorp.com/t/count-inside-modules-bug/27553 "2021-07-30T09:03:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![schollii](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/schollii/32/16958_2.png) [@schollii](https://discuss.hashicorp.com/u/schollii)\
**Post date:** [July 30, 2021, 9:03am UTC](https://discuss.hashicorp.com/t/count-inside-modules-bug/27553/1 "2021-07-30T09:03:53Z")

</div>

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:

```auto
data "kubectl_file_documents" "xray_daemon" {
    content = templatefile("${path.module}/xray-templ.yaml",
    {
      namespace = var.namespace
      role_name = aws_iam_role.xray_daemon.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():

```auto
Error: Invalid count argument
│ 
│ on /deployment-tool/templates/cluster-config/schema-2.1/creation-module/aws-xray/main.tf 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](https://github.com/gavinbunney/terraform-provider-kubectl/issues/58). 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.

---

<div class="post-metadata">

**Author:** ![willw1](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/willw1/32/24138_2.png) [@willw1](https://discuss.hashicorp.com/u/willw1)\
**Post date:** [September 27, 2022, 5:29pm UTC](https://discuss.hashicorp.com/t/count-inside-modules-bug/27553/2 "2022-09-27T17:29:06Z")

</div>

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.
