<p>This was working perfectly fine before but for some reason it no longer is, would appreciate if someone can help fix this:</p>
<p>My terraform code as follows, have replaced key info. with &quot;&lt;&gt;&quot; just for sharing publicly here:</p>
<p>Outer main.tf has this:</p>
<pre><code>    module &quot;&lt;name&gt;_service_account&quot; {
  source = &quot;../modules/kubernetes/service-account&quot;
  name   = &quot;&lt;name&gt;-deployer&quot;
}

# Create &lt;name&gt; platform namespace
resource &quot;kubernetes_namespace&quot; &quot;&lt;name&gt;-platform&quot; {
  metadata {
    name = &quot;&lt;name&gt;-platform&quot;
  }
}
</code></pre>
<p>The service account main.tf module:</p>
<pre><code>resource &quot;kubernetes_service_account&quot; &quot;serviceaccount&quot; {
  metadata {
    name      = var.name
    namespace = &quot;kube-system&quot;
  }
}


resource &quot;kubernetes_cluster_role_binding&quot; &quot;serviceaccount&quot; {
  metadata {
    name = var.name
  }

  subject {
    kind = &quot;User&quot;
    name = &quot;system:serviceaccount:kube-system:${var.name}&quot;
  }

  role_ref {
    kind      = &quot;ClusterRole&quot;
    name      = &quot;cluster-admin&quot;
    api_group = &quot;rbac.authorization.k8s.io&quot;
  }
}

data &quot;kubernetes_service_account&quot; &quot;serviceaccount&quot; {
  metadata {
    name = var.name
    namespace = &quot;kube-system&quot;
  }
  depends_on = [
    resource.kubernetes_service_account.serviceaccount
  ]
}

data &quot;kubernetes_secret&quot; &quot;serviceaccount&quot; {
  metadata {
    name = data.kubernetes_service_account.serviceaccount.default_secret_name
    namespace = &quot;kube-system&quot;
  }
  binary_data = {
    &quot;token&quot;: &quot;&quot;
  }
  depends_on = [
    resource.kubernetes_service_account.serviceaccount
  ]
}
</code></pre>
<p>My outputs.tf for the above module:</p>
<pre><code>output &quot;secret_token&quot; {
    sensitive = true
    value = lookup(data.kubernetes_secret.serviceaccount.binary_data, &quot;token&quot;)
}
</code></pre>
<p>The error that I get in my terraform pipeline:</p>
<pre><code>│ Error: Unable to fetch service account from Kubernetes: serviceaccounts &quot;&lt;name&gt;-deployer&quot; not found
│ 
│   with module.&lt;name&gt;_service_account.data.kubernetes_service_account.serviceaccount,
│   on ../modules/kubernetes/service-account/main.tf line 27, in data &quot;kubernetes_service_account&quot; &quot;serviceaccount&quot;:
│   27: data &quot;kubernetes_service_account&quot; &quot;serviceaccount&quot; {
</code></pre>

terraform-ftw
<p>Figured it out, this is a new environment/project and I had the terraform refresh stage still in the pipeline hence why it couldnt find the service account, removing that and just letting the plan and apply run first solved it.</p>
