<p>I am creating my very own EKS cluster and had some issues adding in roles via my namespace declared in locals dynamically
I am using the <a href="https://github.com/aidanmelen/terraform-aws-eks-auth/tree/v1.0.0" rel="nofollow noreferrer">terraform-aws-eks-auth Tag v1.0.0</a> to populate my IAM roles into the groups that i have created in terraform.</p>
<p>I expect that each.key variable would populate/append in the list of maps entries of map_roles but so far it doesn't do so. The end result in aws auth config map shows that it only contains AdminAccessRole, my_eks_admin_role, dev-kiwi-ingress-nginx_admin_role and dev-kiwi-ingress-nginx_readonly_role. The previous namespaces that was supposed to be dynamically created were overwritten.</p>
<p>Should I even use for_each in such case? How can I populate aws-auth dynamically by namespaces used, combined with 2 static entries.</p>
<p>I appreciate any advice or help!</p>
<pre><code>locals {
  namespaces = [
    &quot;${var.env}-devops&quot;,
    &quot;${var.env}-devops-ingress-nginx&quot;,
    &quot;${var.env}-shared&quot;,
    &quot;${var.env}-shared-ingress-nginx&quot;,
    &quot;${var.env}-pear&quot;,
    &quot;${var.env}-pear-ingress-nginx&quot;,
    &quot;${var.env}-apple&quot;,
    &quot;${var.env}-apple-ingress-nginx&quot;,
    &quot;${var.env}-banana&quot;,
    &quot;${var.env}-banana-ingress-nginx&quot;,
    &quot;${var.env}-kiwi&quot;,
    &quot;${var.env}-kiwi-ingress-nginx&quot;
  ]
}

module &quot;eks_auth&quot; {
  source = &quot;./terraform-modules/terraform-aws-eks-auth-1.0.0&quot;
  eks    = module.eks
  # Additional IAM roles to add to the aws-auth configmap.
  for_each = toset(local.namespaces)
  map_roles = [
    {
      rolearn  = &quot;arn:aws:iam::1234567890:role/AdminAccessRole&quot;
      username = &quot;AdminAccessRole&quot;
      groups   = [&quot;system:masters&quot;]
    },
    {
      rolearn  = &quot;arn:aws:iam::1234567890:role/my-eks-admin-role&quot;
      username = &quot;my_eks_admin_role&quot;
      groups   = [&quot;system:masters&quot;]
    },
    {
      rolearn  = aws_iam_role.eks_namespace_admin_roles[each.key].arn
      username = &quot;${each.key}_admin_role&quot;
      groups   = [&quot;${each.key}:${each.key}_group&quot;]
    },
    {
      rolearn  = aws_iam_role.eks_namespace_readonly_roles[each.key].arn
      username = &quot;${each.key}_readonly_role&quot;
      groups   = [&quot;${each.key}:${each.key}_group&quot;]
    }
  ]

  map_users = [
    {
      userarn  = &quot;arn:aws:iam::1234567890:user/terraform-service-account&quot;
      username = &quot;terraform-service-account&quot;
      groups   = [&quot;system:masters&quot;]
    }
  ]
}
</code></pre>
<p>Terraform v1.0.11
aws provider version = &quot;~&gt; 4.9.0&quot;</p>

Tsu Wei Quan
<p>By adding the <code>for_each</code> in the level shown in the question, terraform will attempt to create multiple instances of the <code>eks_auth</code> module</p>
<p>In your case you need to apply the looping in the variable creation.
Also in order to not loop through the static definitions we use <code>concat</code> to merge the lists E.g.</p>
<pre><code>  map_roles = concat( [ for index,value in toset(local.namespaces) :
   {
      rolearn  = aws_iam_role.eks_namespace_admin_roles[value].arn
      username = &quot;${value}_admin_role&quot;
      groups   = [&quot;${value}:${value}_group&quot;]
   },
   {
      rolearn  = aws_iam_role.eks_namespace_readonly_roles[value].arn
      username = &quot;${value}_readonly_role&quot;
      groups   = [&quot;${value}:${value}_group&quot;]
   }
  ], 
  [        
   {
       rolearn  = &quot;arn:aws:iam::1234567890:role/AdminAccessRole&quot;
       username = &quot;AdminAccessRole&quot;
       groups   = [&quot;system:masters&quot;]
   },
   {
      rolearn  = &quot;arn:aws:iam::1234567890:role/my-eks-admin-role&quot;
      username = &quot;my_eks_admin_role&quot;
      groups   = [&quot;system:masters&quot;]
   }
 ])
</code></pre>
