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

Terraform使用同map值时出现Inconsistent conditional result types错误如何解决?

问题根源

报错的核心原因是你内层for循环的三元判断分支返回值类型不统一,和你定义的local.defined_ranges结构无关:

  • 当source == "#Env"为真时,你返回的是replace(source, "#Env", env)的执行结果,这是字符串类型(比如env为prod时返回值为"prod")
  • 其余判断分支返回的是local.defined_ranges的属性值,是列表类型(list(string),比如["192.168.2.0/24"])

Terraform是强类型语言,要求三元运算符的真假分支返回值类型必须完全一致,所以直接触发类型不匹配报错。

除此之外你的代码逻辑还存在嵌套错误:内层循环返回的结果既有字符串又有列表,外层循环还要用这些结果作为key去读取local.defined_ranges的属性,但列表无法作为Map的查询键,就算类型问题修复,后续也会报错。

修复方案

你不需要嵌套两层for循环,直接在一层循环里处理所有判断逻辑即可,保证所有分支都返回local.defined_ranges的属性(统一为list(string)类型):

source_ranges = distinct(flatten([
  for source in v.source_ranges_names :
  # 匹配#Env直接返回对应环境的预定义范围
  source == "#Env" ? local.defined_ranges[env] :
  # 匹配#Env_customer_cidr直接拼接环境名取对应范围
  length(regexall("#Env_customer_cidr", source)) > 0 ? local.defined_ranges["${env}_customer_cidr"] :
  # 其余情况直接取对应key的预定义范围
  local.defined_ranges[source]
]))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 13:15:02