<p>I am using terraform to create Kubernetes namespace. Sample below</p>
<pre><code>resource &quot;kubernetes_namespace&quot; &quot;test1&quot; {
  metadata {
    name = local.ns_name
  }

}
</code></pre>
<p>I am trying to create Blue/Green kind of deployment using terraform following this <a href="https://www.hashicorp.com/blog/terraform-feature-toggles-blue-green-deployments-canary-test" rel="nofollow noreferrer">link</a>. As part of it, I have created two kubernetes clusters now. One of blue and other for green side and there by now I have two kubernetes providers</p>
<pre><code>provider &quot;kubernetes&quot; {
  alias                  = &quot;kubernetes_blue&quot;
}

provider &quot;kubernetes&quot; {
  alias                  = &quot;kubernetes_green&quot;
}
</code></pre>
<p>I want to understand if there is a way, I can have some conditional on the <code>kubernetes_namespace</code> such that, depending on the flags <code>var.enable_green_side</code> and <code>var.enable_blue_side</code>, I can create the same namespace in multiple kubernetes clusters without having to repeat the entire resource block as follows</p>
<pre><code>resource &quot;kubernetes_namespace&quot; &quot;test1&quot; {
  metadata {
    name = local.ns_name
  }

  provider = kubernetes.kubernetes_blue  
}

resource &quot;kubernetes_namespace&quot; &quot;test2&quot; {
  metadata {
    name = local.ns_name
  }

  provider = kubernetes.kubernetes_green  
}
</code></pre>
<p>Thanks in advance.</p>

<p>Terraform's model requires that each <code>resource</code> block belong to exactly one provider configuration, so there is no way to avoid declaring the resource twice, but you can at least reduce the amount of duplication that causes by factoring it out into a module and calling that module twice, rather than by duplicating the <code>resource</code> block directly:</p>
<pre><code>provider &quot;kubernetes&quot; {
  alias = &quot;blue&quot;
}

provider &quot;kubernetes&quot; {
  alias = &quot;green&quot;
}

module &quot;blue&quot; {
  source = &quot;../modules/bluegreen&quot;

  # (any settings the module needs from the root)

  providers = {
    kubernetes = kubernetes.blue
  }
}

module &quot;blue&quot; {
  source = &quot;../modules/bluegreen&quot;

  # (any settings the module needs from the root)

  providers = {
    kubernetes = kubernetes.green
  }
}
</code></pre>
<p><a href="https://www.terraform.io/docs/language/meta-arguments/module-providers.html" rel="nofollow noreferrer">The special <code>providers</code> argument</a> in a <code>module</code> block allows you to give the child module a different &quot;view&quot; of the declared provider configurations than the caller has. In the <code>module &quot;blue&quot;</code> block above, the <code>providers</code> argument is saying: &quot;Inside this module instance, any reference to the default <code>kubernetes</code> provider configuration means to use the <code>kubernetes.blue</code> configuration from the caller&quot;.</p>
<p>Inside the module then you can just write normal <code>resource &quot;kubernetes_....&quot;</code> blocks without any special <code>provider</code> arguments, because that'll cause them to attach to the default provider <em>from the perspective of that module instance</em>, and each of the two module instances has a different configuration bound to that.</p>
<p>Whether this factoring out into a module will be helpful will of course depend on how much context from the calling module the child module ends up needing. If your <code>module</code> block ends up with almost as many arguments inside it as the <code>resource</code> block(s) you're factoring out then it'd likely be better to just keep the <code>resource</code> blocks at the top level and avoid the indirection.</p>
