Terraform动态块报Missing key/value separator错误,求助排查
问题排查与修复
错误根源
你的代码存在两处关键问题:
- map推导式箭头符号错误:Terraform的
for表达式中,键值对分隔必须用原生的=>,但你代码里写的是HTML转义后的=>,Terraform无法识别该符号,直接触发"Missing key/value separator"错误。 - 变量名不匹配:生成map键时使用了
s.scaling,但content块里赋值的是scaling_adjustment = step_adjustment.value.scaling_adjustment,说明输入变量step_scaling_policy.step_adjustment的字段应为scaling_adjustment而非scaling,此处不匹配会导致键生成逻辑出错。
修复后的代码
step_scaling_policy_configuration = var.step_scaling_policy == null ? null : { dynamic "step_adjustment" { for_each = {for s in var.step_scaling_policy.step_adjustment : format("%s-%s-%s", s.upper_bound, s.lower_bound, s.scaling_adjustment) => s} content { metric_interval_lower_bound = step_adjustment.value.lower_bound metric_interval_upper_bound = step_adjustment.value.upper_bound scaling_adjustment = step_adjustment.value.scaling_adjustment } } }
额外注意事项
- 确保
var.step_scaling_policy.step_adjustment列表的每个元素都包含lower_bound、upper_bound、scaling_adjustment三个字段,否则会触发字段不存在的错误。 - 若
upper_bound或lower_bound可能为null,建议在format函数中添加空值处理,避免生成无效键名,示例:
format("%s-%s-%s", coalesce(s.upper_bound, "null"), coalesce(s.lower_bound, "null"), s.scaling_adjustment)
内容的提问来源于stack exchange,提问作者Mikasa Ackerman
相关产品推荐
相关产品推荐

