Terraform:EKS 模块的动态嵌套组

发布于 2025-01-09 20:27:16 字数 2212 浏览 1 评论 0原文

我正在尝试使用 Terraform AWS EKS 模块在不同环境中配置 EKS (https ://registry.terraform.io/modules/terraform-aws-modules/eks/)。环境因 EKS 托管节点组的数量而异。我使用对象列表来描述变量中的节点组:

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

这是节点组定义的模块代码:

  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
    }
  }

但是 terraform plan 返回错误:

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.

在日志中我看到很多以下消息: [WARN] ReferenceTransformer:找不到引用:“each.value”

有人可以帮我解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2025-01-16 20:27:16

根据OP的评论,我能够创建一个动态eks_management_node_groups对象。完整的解决方案如下所示:

variable "workers" {
  type = list(object({
    name          = string
    image         = string
    instances     = list(string)
    capacity_type = optional(string)
  }))
  default = [{
    name          = "system"
    image         = "AL2023_x86_64_STANDARD"
    instances     = ["t3.small", "t3a.small"]
    capacity_type = "ON_DEMAND"
  }]
}

locals {
  vpc_cidr     = "10.0.0.0/16"
  azs          = slice(data.aws_availability_zones.available.names, 0, 3)
  cluster_name = "${var.base_name}-${data.aws_region.current.name}"
  eks_nodegroups = { for node_group in var.workers : node_group.name => {
    name              = "${var.base_name}-${node_group.name}"
    iam_role_name     = "${var.base_name}-${node_group.name}-eks-node-group"
    ami_type          = node_group.image
    instance_types    = node_group.instances
    desired_size      = 1
    min_size          = 1
    max_size          = 3
    enable_monitoring = false
    capacity_type     = try(node_group.capacity_type, "ON_DEMAND")
  }}
}

您可以使用其他 terrafrom 函数来创建可选值的默认值和后备值。生成托管节点对象后,您可以通过设置 eks_management_node_groups = local.eks_nodegroups 将其传递给 eks 模块

Based on OP's comment I was able to create a dynamic eks_managed_node_groups object. The complete solution would look something like this:

variable "workers" {
  type = list(object({
    name          = string
    image         = string
    instances     = list(string)
    capacity_type = optional(string)
  }))
  default = [{
    name          = "system"
    image         = "AL2023_x86_64_STANDARD"
    instances     = ["t3.small", "t3a.small"]
    capacity_type = "ON_DEMAND"
  }]
}

locals {
  vpc_cidr     = "10.0.0.0/16"
  azs          = slice(data.aws_availability_zones.available.names, 0, 3)
  cluster_name = "${var.base_name}-${data.aws_region.current.name}"
  eks_nodegroups = { for node_group in var.workers : node_group.name => {
    name              = "${var.base_name}-${node_group.name}"
    iam_role_name     = "${var.base_name}-${node_group.name}-eks-node-group"
    ami_type          = node_group.image
    instance_types    = node_group.instances
    desired_size      = 1
    min_size          = 1
    max_size          = 3
    enable_monitoring = false
    capacity_type     = try(node_group.capacity_type, "ON_DEMAND")
  }}
}

You can use additional terrafrom functions to create defaults and fallbacks for optional values. Once your managed nodes object is generated you can just pass it to the eks module by setting eks_managed_node_groups = local.eks_nodegroups

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文