<p>I'm unsure what I'm doing wrong.
Trying to create role and role binding in terraform.</p>
<p>I have terraform as such:</p>
<pre><code>resource &quot;kubernetes_cluster_role&quot; &quot;allow_port_forward&quot; {
  count = (
    (var.env != &quot;prod&quot;)
    &amp;&amp; (var.env != &quot;infra&quot;)
  ) ? 1 : 0
  metadata {
    annotations = {
      &quot;CCE.com/IAM&quot; : &quot;true&quot;
    }
    name = &quot;allow-port-forward&quot;
  }

  rule {
    api_groups = [&quot;rbac.authorization.k8s.io&quot;]
    resources  = [&quot;pods&quot;, &quot;pods/portforward&quot;]
    verbs      = [&quot;get&quot;, &quot;list&quot;, &quot;create&quot;]
  }
}

resource &quot;kubernetes_cluster_role_binding&quot; &quot;allow_port_forward&quot; {
  count = (
    (var.env != &quot;prod&quot;)
    &amp;&amp; (var.env != &quot;infra&quot;)
  ) ? 1 : 0
  metadata {
    annotations = {
      &quot;CCE.com/IAM&quot; : &quot;true&quot;
    }
    name = &quot;allow-port-forward&quot;
  }

  role_ref {
    api_group = &quot;rbac.authorization.k8s.io&quot;
    kind      = &quot;ClusterRole&quot;
    name = kubernetes_cluster_role.allow_port_forward.metadata[0].name

  }
  subject {
    # developer group
    kind      = &quot;Group&quot;
    name      = &quot;0f2762d3df0025dd3f71c00207c49c39&quot;
    api_group = &quot;rbac.authorization.k8s.io&quot;
  }
}
</code></pre>
<p>Terraform plan returns this. it complains about count, but unsure what to do with it.</p>
<pre><code>Error: Missing resource instance key
│ 
│   on modules/rbac/cluster_role_binding/main.tf line 98, in resource &quot;kubernetes_cluster_role_binding&quot; &quot;allow_port_forward&quot;:
│   98:     name = kubernetes_cluster_role.allow_port_forward.metadata[0].name
│ 
│ Because kubernetes_cluster_role.allow_port_forward has &quot;count&quot; set, its attributes must be accessed on specific instances.
│ 
│ For example, to correlate with indices of a referring resource, use:
│     kubernetes_cluster_role.allow_port_forward[count.index]
</code></pre>

Izito
<p>Since you are creating the <code>kubernetes_cluster_role</code> resource with the count meta-argument:</p>
<pre><code>  count = (
    (var.env != &quot;prod&quot;)
    &amp;&amp; (var.env != &quot;infra&quot;)
  ) ? 1 : 0
</code></pre>
<p>that means if the condition is true, there will be exactly one instance of the <code>kubernetes_cluster_role</code>. Now, the <code>count</code> meta-argument adds indexes, so in order to reference any of the resources created with <code>count</code>, you need either to know the exact index or to use <code>count.index</code> object. Since the second resource, i.e., <code>kubernetes_cluster_role_binding</code>, is using the same <code>count</code> meta-argument logic, you can fix this issue with:</p>
<pre><code>resource &quot;kubernetes_cluster_role_binding&quot; &quot;allow_port_forward&quot; {
  count = (
    (var.env != &quot;prod&quot;)
    &amp;&amp; (var.env != &quot;infra&quot;)
  ) ? 1 : 0
.
.
.

  role_ref {
    api_group = &quot;rbac.authorization.k8s.io&quot;
    kind      = &quot;ClusterRole&quot;
    name = kubernetes_cluster_role.allow_port_forward[count.index].metadata[0].name
  }
.
.
.
}
</code></pre>
<p>The <a href="https://developer.hashicorp.com/terraform/language/meta-arguments/count" rel="nofollow noreferrer">documentation</a> has some pretty good examples.</p>
