我正在尝试使用 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: reference not found: "each.value"
有人可以帮我吗?
根据OP的评论,我能够创建一个动态的
eks_managed_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_managed_node_groups = local.eks_nodegroups
将其传递给 eks 模块