<p>I am trying to create fargate profiles for EKS using terraform, the requirement is to create multiple fargate profiles bound to single namespace but different label.</p>
<p>I have defined the selector variable as below :</p>
<pre><code>variable &quot;selectors&quot; {
  description = &quot;description&quot;
  type = list(object({
    namespace    = string
    labels       = any
  }))
  default     = []
}
</code></pre>
<p>and the fargate module block as below :</p>
<pre><code>resource &quot;aws_eks_fargate_profile&quot; &quot;eks_fargate_profile&quot; {      
  for_each               = {for namespace in var.selectors: namespace.namespace =&gt; namespace}
  cluster_name           = var.cluster_name
  fargate_profile_name   = format(&quot;%s-%s&quot;,&quot;fargate&quot;,each.value.namespace)
  pod_execution_role_arn = aws_iam_role.eks_fargate_role.arn
  subnet_ids             = var.vpc_subnets

  selector {
    namespace = each.value.namespace
    labels    = each.value.labels
  }
</code></pre>
<p>and calling the module as below :</p>
<pre><code>  selectors = [
      {
        namespace = &quot;ns&quot;
        labels = {           
          Application = &quot;fargate-1&quot;
          }
      },
      {
        namespace = &quot;ns&quot;
        labels = {
          Application = &quot;fargate-2&quot;
          }
      }
    ]
</code></pre>
<p>When i try to run terraform plan, i am getting below error :</p>
<pre><code>Two different items produced the key &quot;jenkinsbuild&quot; in this 'for' expression. If duplicates are expected, use the ellipsis (...) after the value expression to enable grouping by key.
</code></pre>
<p>I tried giving (...) at the end of the for loop, this time i am getting another error as below :</p>
<pre><code>each.value is tuple with 1 element
│
│ This value does not have any attributes.
</code></pre>
<p>I also defined selectors variable type as <em><strong>any</strong></em>, as well tried type casting the output to string(namespace) and object(labels), but no luck.</p>
<p>So could you please help me in achieving the same, It seems i am close but i am missing something here.</p>
<p>Thanks and Regards,
Sandeep.</p>

sandeepdosapati
<p>In Terraform, when using <code>for_each</code>, the keys must be unique. If you do not have unique keys, then use <code>count</code>:</p>
<pre><code>resource &quot;aws_eks_fargate_profile&quot; &quot;eks_fargate_profile&quot; {      
  count = length(var.selectors)

  selector {
    namespace = var.selectors[count.index].namespace
    labels    = var.selectors[count.index].labels
  }

  ...
}
</code></pre>
