<p>I created this script:</p>
<pre><code>provider &quot;kubernetes&quot; {
  host = &quot;https://${var.cluster_url}:8443&quot;
  client_certificate     = file(&quot;./admin.crt&quot;)
  client_key             = file(&quot;.admin.key&quot;)
  cluster_ca_certificate = file(&quot;./ca.crt&quot;)
}
resource &quot;kubernetes_namespace&quot; &quot;resource&quot; {
  metadata {
    annotations = {
      name = &quot;exampleannotation&quot;
    }

    labels = {
      mylabel = &quot;labelvalue&quot;
    }

    name = &quot;${var.namespace_name}&quot;
  }
}
</code></pre>
<p>If I run it twice, before running the second time, it removes the previous resource (see below).</p>
<p>I understand that, under the hood, terraform is tracking the resources it's created previously for that script in a state file.
However I was wondering if there is any way to avoid the removal?</p>
<pre><code>FIRST RUN:

 terraform apply  -var=&quot;namespace_name=pippo&quot; -var=&quot;cluster_url=xxx.yyyy.zzz&quot;

kubernetes_namespace.resource: Creating...
kubernetes_namespace.resource: Creation complete after 0s [id=pippo]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

SECOND RUN:

terraform apply  -var=&quot;namespace_name=pluto&quot; -var=&quot;cluster_url=xxx.yyyy.zzz&quot;

kubernetes_namespace.resource: Destroying... [id=pippo]
kubernetes_namespace.resource: Destruction complete after 6s
kubernetes_namespace.resource: Creating...
kubernetes_namespace.resource: Creation complete after 0s [id=pluto]


Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
</code></pre>

Gian Filippo Maniscalco
<p>In this case you already had the right intuition: The Terraform resource (called &quot;resource&quot; in your example) had a name change. Given that a name uniquely identifies a namespace in Kubernetes the only way to change a name is by removing the old namespace and creating a new one.</p>
<p>If you want to keep two namespaces, you need two resources in Terraform. E.g.:</p>
<pre><code>resource &quot;kubernetes_namespace&quot; &quot;resource&quot; {
  for_each = toset([&quot;pippo&quot;, &quot;pluto&quot;])
  metadata {
    annotations = {
      name = &quot;exampleannotation&quot;
    }

    labels = {
      mylabel = &quot;labelvalue&quot;
    }

    name = each.key
  }
}
</code></pre>
<p>Note that <code>for_each</code> is only available after Terraform 0.12.</p>
