Terraform: dynamic nested groups for EKS module

Viewed 309

I'm trying to configure EKS on different environments with Terraform AWS EKS module (https://registry.terraform.io/modules/terraform-aws-modules/eks/). Environments differ by number of EKS-managed node groups. I'm using list of objects to describe nodegroups in a varibale:

node_groups = [
  {
    "name"             = "nodegroup_name"
    "desired_capacity" = 1,
    "max_capacity"     = 3,
    "min_capacity"     = 1,
    "subnets"          = "internal",
    "instance_types"   = "r5.xlarge",
    "k8s_labels"       = {
      NodeGroup        = "internal"
    }
  }
]

And here's the module code for node groups definition:

  for_each = { for node_group in var.node_groups : node_group.name => node_group }
  eks_managed_node_groups               = {
    nodegroup={
      name                              = each.key
      desired_capacity                  = each.value.desired_capacity
      max_capacity                      = each.value.max_capacity
      min_capacity                      = each.value.min_capacity
      subnets                           = each.value.subnets != "external" ? data.aws_subnets.eks_external_subnets.ids : data.aws_subnets.eks_internal_subnets.ids
      instance_types                    = [each.value.instance_types]
      source_security_group_ids         = each.value.subnets != "external" ? [ aws_security_group.eks-external-sec-group.id ] : [ aws_security_group.eks-internal-sec-group.id ]
      capacity_type                     = "ON_DEMAND"
      k8s_labels                        = each.value.k8s_labels
      additional_tags                   = local.tags
    }
  }

But terraform plan returns errors:

Error: Incorrect attribute value type

  on ../../../modules/aws/eks/main.tf line 66, in data "aws_eks_cluster_auth" "this":
  66:   name = module.eks_remote.cluster_id

Inappropriate value for attribute "name": string required.


Error: Invalid function argument

  on ../../../modules/aws/eks/main.tf line 101, in locals:
 101:   ${chomp(module.eks_remote.aws_auth_configmap_yaml)}

Invalid value for "str" parameter: string required.

And in logs I see a lot of the following messages: [WARN] ReferenceTransformer: reference not found: "each.value"

Can somebody, please, help me with this?

0 Answers
Related