<p>If I have read this correctly you are trying to target different clusters with your provider so you can then use a helm_release.</p>
<p>You need to have distinct providers for each cluster and differentiate them with an <code>alias</code>  so.</p>
<pre><code>provider &quot;helm&quot; {
  kubernetes {    
    host  = &quot;https://my_endpoint1&quot;
    token = &quot;provider.access_token1&quot;
    cluster_ca_certificate = &quot;base64encodedcerthere1&quot;
  }
}

provider &quot;helm&quot; {
  alias = &quot;cluster2&quot;
  kubernetes {    
    host  = &quot;https://my_endpoint2&quot;
    token = &quot;provider.access_token2&quot;
    cluster_ca_certificate = &quot;base64encodedcerthere2&quot;
  }
}
</code></pre>
<p>the first provider will be used where no alias is provided and the second used only where an alias is used for the provider so for example:</p>
<pre><code>resource &quot;helm_release&quot; &quot;example&quot; {
  name       = &quot;my-bob-release&quot;
  ...
}
</code></pre>
<p>the above uses the default un-named provider</p>
<pre><code>resource &quot;helm_release&quot; &quot;example&quot; {
  provider   = helm.cluster2
  name       = &quot;my-bob-release&quot;
  ...
}
</code></pre>

CC BY-SA 4.0
2021-07-13T11:13:28.040Z
2021-07-13T11:13:28.040Z
krystan honour
<p>I'm new to <code>Terraform</code>, I need suggestion about handling one case where I need to use data from <code>counted</code> <code>data-source</code> to <code>un-counted</code> <code>provider</code>.</p>
<p>I need to fetch the <code>gke-cluster</code> context and pass it to <code>provider helm/kubernetes</code> to provision resources on it .
There can be multiple <code>gke -clusters</code>, so I'm using <code>count</code> on the <code>data-source  google_container_cluster</code> and then I was trying to get the <code>endpoint</code> from it .
Like below :</p>
<pre><code>data &quot;google_container_cluster&quot; &quot;my_cluster&quot; {
    count      = var.enabled ? length(var.gke_clusters) : 0
    location   = lookup(var.gke_clusters[count.index], &quot;cluster_location&quot;)
    name   = lookup(var.gke_clusters[count.index], &quot;cluster_name&quot;)
}
provider &quot;helm&quot; {
  kubernetes {    
    host  = &quot;https://${data.google_container_cluster.my_cluster.endpoint}&quot;
    token = data.google_client_config.provider.access_token
    cluster_ca_certificate = base64decode(
    data.google_container_cluster.my_cluster.master_auth[0].cluster_ca_certificate,
    )    
  }
}
</code></pre>
<p>But this does not work because when I'm referring to <code>my_cluster</code> I have to specify the <code>index</code>  , and if I specify the index <code>my_cluster[count.index]</code>, I get an error that <code>Error: Reference to &quot;count&quot; in non-counted context</code>.</p>
<p>Thanks in advance.</p>

<terraform><terraform-module><google-kubernetes-engine>
Using counted data source in non-counted provider block in Terraform
Learner
# Using counted data source in non-counted provider block in Terraform

(Asked by: username_0 on 2021-07-02)

I'm new to Terraform, I need suggestion about handling one case where I need to use data from counted data-source to un-counted provider.
I need to fetch the gke-cluster context and pass it to provider helm/kubernetes to provision resources on it .
There can be multiple gke -clusters, so I'm using count on the data-source  google_container_cluster and then I was trying to get the endpoint from it .
Like below :
data "google_container_cluster" "my_cluster" {
    count      = var.enabled ? length(var.gke_clusters) : 0
    location   = lookup(var.gke_clusters[count.index], "cluster_location")
    name   = lookup(var.gke_clusters[count.index], "cluster_name")
}
provider "helm" {
  kubernetes {    
    host  = "https://${data.google_container_cluster.my_cluster.endpoint}"
    token = data.google_client_config.provider.access_token
    cluster_ca_certificate = base64decode(
    data.google_container_cluster.my_cluster.master_auth[0].cluster_ca_certificate,
    )    
  }
}

But this does not work because when I'm referring to my_cluster I have to specify the index  , and if I specify the index my_cluster[count.index], I get an error that Error: Reference to "count" in non-counted context.
Thanks in advance.

**Tags:** terraform, terraform-module, google-kubernetes-engine


---

## 1 answers

### Answer by username_1 on 2021-07-13. Score: 1

If I have read this correctly you are trying to target different clusters with your provider so you can then use a helm_release.
You need to have distinct providers for each cluster and differentiate them with an alias  so.
provider "helm" {
  kubernetes {    
    host  = "https://my_endpoint1"
    token = "provider.access_token1"
    cluster_ca_certificate = "base64encodedcerthere1"
  }
}

provider "helm" {
  alias = "cluster2"
  kubernetes {    
    host  = "https://my_endpoint2"
    token = "provider.access_token2"
    cluster_ca_certificate = "base64encodedcerthere2"
  }
}

the first provider will be used where no alias is provided and the second used only where an alias is used for the provider so for example:
resource "helm_release" "example" {
  name       = "my-bob-release"
  ...
}

the above uses the default un-named provider
resource "helm_release" "example" {
  provider   = helm.cluster2
  name       = "my-bob-release"
  ...
}


---

