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

Terraform读取JSON生成告警策略时path参数无效问题排查

Terraform读取JSON模板报错:Invalid value for 'path' parameter 原因及解决

错误原因

你在代码里错误地将file()函数嵌套进了templatefile()中:

  • templatefile()的第一个参数要求传入文件路径,它会自动读取文件内容并完成变量替换
  • 而file(files)返回的是JSON文件的原始内容字符串,Terraform把这个字符串当作文件路径去解析,自然会触发「路径参数无效」的错误

解决方法

直接将fileset返回的文件路径传给templatefile(),去掉多余的file()嵌套。修正后的代码如下:

locals{
  json_file = fileset(path.module, "alt/*.json")

  alert_policies = {
    for files in local.json_file : files => jsondecode(templatefile(files, {
      tf_monitored_project = "test"
      tf_alert_name        = title("Alert for ${var.env}")
    }))
  }
}

额外说明

  • templatefile()会自动读取目标文件内容,识别并替换其中的${变量名}占位符
  • 替换完成后,再用jsondecode()将处理后的字符串转为Terraform可识别的JSON对象,后续就能用来构建google_monitoring_alert_policy资源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:10:19