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

Terraform CloudFront模块条件结果类型不一致错误求助

Terraform 条件表达式类型不一致问题解决

问题场景

作为Terraform新手,遇到以下类型不匹配错误,请求排查解决:

错误信息

Error: Inconsistent conditional result types
│
│   on .terraform\modules\frontend_website.cloudfront\main.tf line 17, in resource "aws_cloudfront_distribution" "www_distribution":
│   17:     for_each = length(var.custom_error_response) == 0 ? [] : var.custom_error_response
│     ├────────────────
│     │ var.custom_error_response is object with 4 attributes
│
│ The true and false result expressions must have consistent types. The given
│ expressions are tuple and object, respectively.

相关代码

dynamic "custom_error_response" {
    for_each = length(var.custom_error_response) == 0 ? [] : var.custom_error_response

    content {
      error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", "3000")
      error_code            = lookup(custom_error_response.value, "error_code", "404")
      response_code         = lookup(custom_error_response.value, "response_code", "200")
      response_page_path    = lookup(custom_error_response.value, "response_page_path", "/index.html")
    }
  } 

问题原因

  1. 类型不匹配:条件表达式的两个分支返回类型不一致——空列表[]是元组类型,而var.custom_error_response是单个对象类型,Terraform严格的类型校验会触发错误。
  2. 逻辑错误:length(var.custom_error_response)作用在单个对象上时,返回的是对象的属性数量,而非判断对象是否存在,无法正确实现“空则不创建”的逻辑。
  3. for_each要求:dynamic块的for_each必须接收集合类型(列表、集合、映射),不能直接遍历单个对象。

解决方案

方案1:将变量改为对象列表(推荐)

如果需要支持配置多个自定义错误响应,直接调整变量类型为对象列表:

variable "custom_error_response" {
  type = list(object({
    error_caching_min_ttl = optional(string, "3000")
    error_code            = optional(string, "404")
    response_code         = optional(string, "200")
    response_page_path    = optional(string, "/index.html")
  }))
  default = []
}

修改dynamic块的for_each逻辑:

dynamic "custom_error_response" {
  for_each = var.custom_error_response

  content {
    error_caching_min_ttl = custom_error_response.value.error_caching_min_ttl
    error_code            = custom_error_response.value.error_code
    response_code         = custom_error_response.value.response_code
    response_page_path    = custom_error_response.value.response_page_path
  }
}

当变量为空列表时,for_each直接遍历空集合,不会创建任何custom_error_response块,类型完全匹配,逻辑更清晰。

方案2:保持单个对象变量,统一返回类型

如果仅需单个自定义错误响应,调整条件表达式,将非空对象包装为单元素列表,确保两个分支返回同类型:

dynamic "custom_error_response" {
  for_each = var.custom_error_response == null ? [] : [var.custom_error_response]

  content {
    error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", "3000")
    error_code            = lookup(custom_error_response.value, "error_code", "404")
    response_code         = lookup(custom_error_response.value, "response_code", "200")
    response_page_path    = lookup(custom_error_response.value, "response_page_path", "/index.html")
  }
}

同时修改变量定义,将默认值设为null:

variable "custom_error_response" {
  type = object({
    error_caching_min_ttl = optional(string, "3000")
    error_code            = optional(string, "404")
    response_code         = optional(string, "200")
    response_page_path    = optional(string, "/index.html")
  })
  default = null
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 17:01:25