has anyone got workaround for adding loop in provi...
# terraform
m
has anyone got workaround for adding loop in provider resource? for example, count. I am getting this: The provider argument name "count" is reserved for use by Terraform in a future version. My use case is, i want to create multiple clusters using terraform gke cluster module. Now i am able to do that. Next step is to create kubernetes_storage_class say xfs on each cluster which can be done in terraform via
resource "kubernetes_storage_class" "xfs"
which requires kubernetes provider as :
Copy code
provider "kubernetes" {
  host                   = "https://${module.gke_cluster.host}"
  token                  = module.gke_cluster.token
  cluster_ca_certificate = module.gke_cluster.cluster_ca_cert
}
This part is failing. Error with above setting is:
Copy code
│ Error: Unsupported attribute
│
│   on <http://cluster.tf|cluster.tf> line 18, in provider "kubernetes":
│   18:   host                   = "https://${module.client_cluster.host}"
│     ├────────────────
│     │ module.client_cluster is a list of object, known only after apply
│
│ Can't access attributes on a list of objects. Did you mean to access attribute "host" for a specific element of the list, or across all elements of the list?
When trying with this setting:
Copy code
provider "kubernetes" {
  host                   = "https://${module.client_cluster[0].host}"
  token                  = module.client_cluster[0].token
  cluster_ca_certificate = module.client_cluster[0].cluster_ca_cert
}
Its working fine but this will have endpoint only of 1st cluster ( say i wanted to create 3 cluster) Now i tried adding count in provider so it can get endpoint of each created cluster:
Copy code
provider "kubernetes" {
  count                  = 3
  host                   = "https://${module.client_cluster[count.index].host}"
  token                  = module.client_cluster[count.index].token
  cluster_ca_certificate = module.client_cluster[count.index].cluster_ca_cert
}
This one fails as:
Copy code
The provider argument name "count" is reserved for use by Terraform in a future version.
If anyone has any experience or any workaround, please let me know. Else i have a crude way of doing this as copying pasting as many module block as many clusters we need
t
since you have a set of things, you want to use
for_each
instead of
count
https://developer.hashicorp.com/terraform/language/meta-arguments/for_each
a
t
Another approach might be to make a custom module, which wraps around the gke cluster module and then adds additional features that you need. In other words, make your own abstraction of what a cluster should look like (possibly with some feature toggles (input variables)). Then you have very simple instantiations of your clusters that are still configurable, but "on your own terms".
s
dynamic providers are not currently possible https://support.hashicorp.com/hc/en-us/articles/6304194229267-Dynamic-provider-configuration
This can be done with https://github.com/mineiros-io/terramate - let me know if you’d like to see an example 🙂
251 Views