There are any way to generate block provider from tfvars

I need to setup multiple eks cluster but due to provider kubeclt can’t perform for_each.
So I need to create a file that contains multiple provider block.
There are any way to do this?

Unsure. Don’t believe that kubectl itself supports multiple provider blocks. At least doesn’t look like it: Terraform Registry

Have seen other providers like azurerm support multiple blocks and folks use alias:

provider "azurerm" {}

provider "azurerm" {
  alias = "devopskv"
}

If kubectl supports multiple provider blocks then could just use a loop in ERB to do it.

<% aliases = [nil, "devopskv"] %>
<% aliases.each do |alias| %>
  <% if alias %>
    provider "azurerm" {
      alias = "<%= alias %>"
    }
  <% else %>
    provider "azurerm" {}
  <% end %>
<% end %>

However, it’s pretty ugly. And the former is clearer.

1 Like

It’s ok for this example but is it possible to load the value from tfvars instead fix in code?
<% aliases = [nil, “devopskv”] %>

like this:
<% aliases = fromvar(“kube_list”) %>

Thanks a lot.

Dont think so. Terraform variables values are available at run-time. Template ERB processing occurs at compile-time, before run-time variables are avaiable.

1 Like