Keeping code DRY with multiple providers

I have the following example code:

module "eks" {
  source = ...
}

provider "kubernetes" {
  host = module.eks.cluster_endpoint
  ...
}

Tthe “eks” module requires the “kubernetes” provider, and the “kubernetes” provider requires outputs derived from the “eks” module. I know this isn’t ideal, but it is what it is.

I have a need to create X clusters per environment. The way I typically handle this using for_each on a module in the main stack:

module "eks" {
  for_each = var.eks_clusters
  ...
}

In order for this to work, you also need to create a separate “kubernetes” provider per “eks” instantiation. But, Terraform doesn’t support iteration (for_each, etc) on providers. So, I’m left with the inability to easily provision multiple clusters per environment without separate “module eks” blocks. This is further complicated by the fact that one environment may need only 1 EKS cluster, but another environment may need >1.

How would you handle this in Terraspace?

Unsure.

Could possibly define some helper methods that call the EKS list_clusters and describe_cluster to grab the eks cluster endpoints as part of terraspace building. However, don’t know much about whether or not that is wise in terms of complexity. Just my 2 cents.