You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform模块调用中动态块参数配置报错求助

问题解决:Terraform EKS模块动态生成节点组配置

错误根源分析

  1. dynamic块使用场景错误:terraform-aws-modules/eks/aws模块的self_managed_node_groups是map类型的输入参数,而非资源内部的嵌套块。dynamic块仅适用于资源、数据源等内部的嵌套配置块(如安全组的ingress块),不能直接在模块参数赋值中使用。
  2. 语法错误:原代码试图在dynamic块的content中直接赋值键值对,不符合Terraform的块定义语法规则。

修正后的代码

模块内部eks.tf

module "eks" {
  source  = "terraform-aws-modules/eks/aws"

  # 使用for表达式将列表转换为模块所需的map结构
  self_managed_node_groups = {
    for ng in var.node_groups : ng.name => {
      capacity_rebalance         = true
      use_mixed_instances_policy = true
      desired_size               = ng.gp_instance_count
    }
  }
}

代码说明

  • 利用Terraform的for表达式遍历var.node_groups列表,将每个节点组的name作为map的键。
  • 对每个节点组,构造符合EKS模块要求的配置对象,自动填充capacity_rebalance、use_mixed_instances_policy固定值,以及从变量中获取的desired_size。
  • 最终生成的self_managed_node_groups参数结构完全匹配你期望的输出。

验证结果

上述代码会生成你需要的配置结构:

gp1 = {
  capacity_rebalance = true 
  use_mixed_instances_policy = true 
  desired_size = 4
},
gp2 = {
  capacity_rebalance = true 
  use_mixed_instances_policy = true 
  desired_size = 2
}

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:36:06