You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
type = "Microsoft.ContainerService/managedClusters@2025-10-02-preview"
90
+
type = "Microsoft.ContainerService/managedClusters@2025-10-01"
91
91
parent_id = azurerm_resource_group.this.id
92
92
location = azurerm_resource_group.this.location
93
93
name = "aks-${random_pet.this.id}"
@@ -145,6 +145,8 @@ With that context, let's explore the two viable options for configuring the Helm
145
145
146
146
Both options require retrieving the connection details—the host URL and cluster CA certificate—from the AKS cluster. The AzApi provider doesn't expose these values directly; however, you can use the `azurerm_kubernetes_cluster` data source as a workaround.
147
147
148
+
:::
149
+
148
150
Add the following to the bottom of your `main.tf` file:
149
151
150
152
```hcl
@@ -154,13 +156,11 @@ data "azurerm_kubernetes_cluster" "this" {
154
156
}
155
157
```
156
158
157
-
:::
158
-
159
159
### Option 1: Configure the Helm provider with Azure bearer token authentication
160
160
161
161
The Helm provider's [`kubernetes` block](https://registry.terraform.io/providers/hashicorp/helm/latest/docs#kubernetes-1) supports a [`token` argument](https://registry.terraform.io/providers/hashicorp/helm/latest/docs#token-1) that lets you supply a bearer token directly for authentication.
162
162
163
-
You can obtain access tokens using the Azure CLI. If you're authenticated with `az login`, you can get a token for the AKS resource like this:
163
+
You can obtain short-lived access tokens using the Azure CLI. If you're authenticated with `az login`, you can get a token for the AKS resource like this:
164
164
165
165
```bash
166
166
az account get-access-token --resource 6dae42f8-4368-4678-94ff-3960e28e3630
@@ -172,7 +172,7 @@ The resource ID `6dae42f8-4368-4678-94ff-3960e28e3630` is the well-known applica
172
172
173
173
:::
174
174
175
-
You can use the [`external`provider](https://registry.terraform.io/providers/hashicorp/external/latest/docs) in Terraform to run this command and capture the token as an external data source.
175
+
Combining the above command with the [`external`data source](https://developer.hashicorp.com/terraform/language/data-sources/external) in Terraform allows you to retrieve the token dynamically and use it in the Helm provider configuration.
176
176
177
177
Add the following to your `main.tf` file:
178
178
@@ -182,9 +182,7 @@ data "external" "this" {
182
182
}
183
183
```
184
184
185
-
This command runs the Azure CLI and extracts the access token in JSON format, which you can then use in the Helm provider configuration.
186
-
187
-
Now you have everything needed to configure the Helm provider:
185
+
You can now reference the token in the Helm provider configuration like this:
188
186
189
187
```hcl
190
188
provider "helm" {
@@ -198,12 +196,24 @@ provider "helm" {
198
196
199
197
This configuration uses the host and cluster CA certificate from the AKS cluster data source and gains access to the cluster using the bearer token from the external data source.
200
198
201
-
If you are solely using Azure CLI authentication (e.g., `az login`), this approach works well. However, if you're deploying from a CI/CD pipeline or using service principals or managed identities, the next option is more flexible.
199
+
:::danger
200
+
201
+
Terraform stores the access token in plain text in the state file. Although the token expires after about one hour, this poses a security risk in shared environments or CI/CD pipelines. For production use, secure your state file appropriately or use Option 2 instead.
202
+
203
+
:::
204
+
205
+
For quick local demos, this approach is convenient. For CI/CD pipelines or service principal authentication, the next option is more flexible.
202
206
203
207
### Option 2: Configure the Helm provider to use the exec plugin with kubelogin
204
208
205
209
The Helm provider also supports using the `exec` plugin mechanism to obtain credentials dynamically. This approach is more flexible and works well with various authentication methods supported by [kubelogin](https://azure.github.io/kubelogin/index.html) which is a [Kubernetes client-go credential plugin](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins) for Azure.
206
210
211
+
:::tip
212
+
213
+
This is a more ideal approach because it does not require storing tokens in the Terraform state file. Instead, the Helm provider invokes the specified command to obtain fresh credentials each time it needs to authenticate.
214
+
215
+
:::
216
+
207
217
To use this approach, add the following to your `main.tf`:
208
218
209
219
```hcl
@@ -220,7 +230,7 @@ provider "helm" {
220
230
"--login",
221
231
"azurecli",
222
232
"--server-id",
223
-
"6dae42f8-4368-4678-94ff-3960e28e3630" # Azure Kubernetes Service AAD Server
0 commit comments