Single provider definition and a list of a module

I’ll try to make it as simple and as short as I can.

Essentially I have a module that creates an EKS cluster.
This module can be invoked N times (a list).

To interact with each module I need to declare a kubernetes provider:

provider "kubernetes" {
 host                   = module.my_eks_module[0].eks_cluster_endpoint
 cluster_ca_certificate = base64decode(module.my_eks_module[0].eks_cluster_certificate_authority_data)
 token                  = module.my_eks_module[0].eks_cluster_id
 experiments {
   manifest_resource = true
 exec {
   api_version = ""
   args        = ["eks", "get-token", "--cluster-name", module.my_eks_module[0].cluster_name]
   command     = "aws"

As you can see I added an index [0] but this is just to validate its basic usage for a single cluster.

My question is: what is the proper way to do it? I need to execute some kubernetes commands for each cluster but I don’t know how to declare this provider.

The same happens if I include my_eks_module a single time but with

count                    = var.create_cluster ? 1 : 0

Thank you

You need to split the Terraform code that creates the cluster, and the Terraform code that talks to the cluster into entirely separate Terraform configurations that you run as two separate processes.

This is required, because Terraform does not support using values that are not known until after apply - such as the credentials to access a newly created Kubernetes cluster - as configuration for providers.