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

Terraform Cloud部署Elastic索引模板时JSON格式错误求助

Terraform Elasticstack Provider索引模板创建失败:expected template.0.mappings to be a JSON object

问题分析

报错明确指出template.0.mappings需要是JSON对象,但当前传入的值不符合要求,且伴随unexpected end of JSON input,核心问题有两个:

  • 代码直接将字符串类型的mappings变量传入资源字段,而该字段实际期望JSON对象结构
  • 传入的mappings字符串为空或存在JSON语法不完整的情况

解决方案

方案1:修正变量类型并传入结构化数据

  1. 修改variables.tf中mappings的类型,从string改为any(若映射结构不固定):
variable "index_templates" { # Creation of Index Templates (ixt)
  type = map(object({
    priority                   = number
    index_patterns             = list(string)
    number_of_shards           = number
    number_of_replicas         = number
    lifecycle_name             = string
    refresh_interval           = string
    max_docvalue_fields_search = number
    total_fields_limit         = number
    mappings                   = any # 替换原有的string类型
  }))
}
  1. 传入index_templates变量时,直接用Terraform的map结构定义映射:
index_templates = {
  "test-p1" = {
    priority                   = 100
    index_patterns             = ["test-p1-*"]
    number_of_shards           = 1
    number_of_replicas         = 0
    lifecycle_name             = "test-policy"
    refresh_interval           = "30s"
    max_docvalue_fields_search = 1000
    total_fields_limit         = 2000
    mappings = {
      properties = {
        timestamp = { type = "date" }
        message   = { type = "text" }
        // 按需添加其他字段映射
      }
    }
  }
}

方案2:保留字符串类型但解码为JSON对象

若必须通过字符串传递mappings(比如从外部配置或Terraform Cloud变量传入),在main.tf中对字符串进行JSON解码:

template {
  settings = jsonencode({
    // ... 原有配置不变
  })

  mappings = jsondecode(each.value.mappings) # 添加jsondecode处理
}

注意:传入的字符串必须是完整合法的JSON,例如:

{"properties": {"timestamp": {"type": "date"}, "message": {"type": "text"}}}

额外排查步骤

  • 执行terraform plan,查看test-p1对应的mappings变量值,确认是否为空或存在语法错误
  • 若使用Terraform Cloud工作区变量,检查变量值是否存在换行、未转义引号等问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 02:00:11