<p>I am learning terraform and trying to translate kubernetes infrastructure over to terraform.</p>
<p>I have a terraform script which creates a given <code>namespace</code>, and then creates secrets from local files. Most of the files do not create properly due to the namespace not being created fast enough.</p>
<p>Is there a correct method to create and wait for confirmation of the name space before continuing within the terraform script? Such as <code>depends_on</code>, etc.?</p>
<p>My current approach:</p>
<pre><code>resource &quot;kubernetes_namespace&quot; &quot;namespace&quot; {
  metadata {
    name = &quot;specialNamespace&quot;
  }
}

resource &quot;kubernetes_secret&quot; &quot;api-env&quot; {
  metadata {
    name      = var.k8s_name_api_env
    namespace = &quot;specialNamespace&quot;
  }

  data = {
    &quot;.api&quot; = file(&quot;${path.cwd},${var.local_dir_path_api_env_file}&quot;)
  }
}

resource &quot;kubernetes_secret&quot; &quot;password-env&quot; {
  metadata {
    name      = var.k8s_name_password_env
    namespace = &quot;specialNamespace&quot;
  }

  data = {
    &quot;.password&quot; = file(&quot;${path.cwd},${var.local_dir_path_password_env_file}&quot;)
  }
}

resource &quot;kubernetes_secret&quot; &quot;tls-crt-env&quot; {
  metadata {
    name      = var.k8s_name_tls_crt_env
    namespace = &quot;specialNamespace&quot;
  }

  data = {
    &quot;server.crt&quot; = file(&quot;${path.cwd},${var.local_dir_path_tls_crt_env_file}&quot;)
  }
}

resource &quot;kubernetes_secret&quot; &quot;tls-key-env&quot; {
  metadata {
    name      = var.k8s_name_tls_key_env
    namespace = &quot;specialNamespace&quot;
  }

  data = {
    &quot;server.key&quot; = file(&quot;${path.cwd},${var.local_dir_path_tls_key_env_file}&quot;)
  }
}
</code></pre>

Matt - Block-Farms.io
<p>Since there is a way to get the <code>name</code> property of the <code>metadata</code> from the <code>kubernetes_namespace</code> resource, I would advise going with that. For example, for the <code>kubernetes_secret</code> resource:</p>
<pre><code>resource &quot;kubernetes_secret&quot; &quot;api-env&quot; {
  metadata {
    name      = var.k8s_name_api_env
    namespace = kubernetes_namespace.namespace.metadata[0].name
  }

  data = {
    &quot;.api&quot; = file(&quot;${path.cwd},${var.local_dir_path_api_env_file}&quot;)
  }
}
</code></pre>
<p>Also, note that most of the resources also have the <code>_v1</code> version (e.g., namespace [1], secret [2] etc.), so I would strongly suggest going with those ones.</p>
<hr />
<p>[1] <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace_v1" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace_v1</a></p>
<p>[2] <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1</a></p>
