Terraform 1.4.3: Struggling with Dynamic Kubernetes Namespace Creation Based on Labels
I've spent hours debugging this and I'm prototyping a solution and I've searched everywhere and can't find a clear answer..... I'm using Terraform 1.4.3 to provision Kubernetes resources, and I'm facing issues when trying to dynamically create namespaces based on certain labels from an external source. My goal is to create a namespace for each unique label value found in a list, but I keep encountering an error that seems to stem from the way I'm referencing the labels. Hereβs a snippet of what I've tried: ```hcl variable "label_list" { type = list(string) default = ["dev", "test", "prod"] } resource "kubernetes_namespace" "dynamic" { for_each = toset(var.label_list) metadata { name = each.key } } ``` When I run `terraform apply`, I receive the following error message: ``` Error: Invalid index on main.tf line 6, in resource "kubernetes_namespace" "dynamic": 6: name = each.key This value does not have any indices. ``` After researching, I realized that the problem might be with how `each.key` is being handled in the context of `for_each`. I also tried changing `toset(var.label_list)` to `zipmap(var.label_list, var.label_list)` but that led to a different error. Have I misunderstood how to use `for_each` with a list in this context? Is there a more appropriate way to dynamically create namespaces based on these labels? Any guidance would be greatly appreciated! For context: I'm using Hcl on Linux. Any help would be greatly appreciated! Any examples would be super helpful. I'm working with Hcl in a Docker container on Linux. Is there a simpler solution I'm overlooking?