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

如何将本地JSON文件内容传入Terraform heredoc块并保留格式

你可以直接使用Terraform内置的file()函数读取目标JSON文件内容,替换原有的heredoc块即可,函数会原封不动返回文件的文本内容,完全保留原有格式。

修改后的代码示例如下:

resource "azurerm_policy_definition" "k8s_seccomp_governance" {
  name         = "k8s_seccomp_governance"
  description  = "Kubernetes cluster containers should only use allowed seccomp profiles"
  policy_type  = "Custom"
  mode         = "Microsoft.Kubernetes.Data"
  display_name = "AMPS K8s Seccomp Governance"

  metadata = <<METADATA
    {
    "category": "Kubernetes",
    "version": "1.0.0"
    }

METADATA

  # 直接读取本地JSON文件内容赋值给policy_rule
  policy_rule = file("./policies/seccomp/seccomp_profile_rule.json")
}

补充注意事项:

  • 路径是相对于Terraform执行根目录的相对路径,按照你给出的项目结构,上述路径可以直接匹配到目标文件
  • 如果需要提前校验JSON格式合法性,可以改用jsonencode(jsondecode(file("./policies/seccomp/seccomp_profile_rule.json"))),该写法会先解析JSON再重新生成标准格式化的JSON内容,避免文件内存在非法JSON语法导致部署报错
  • 同目录下的seccomp_parameters.json也可以用同样的方法读取,传入该资源的parameters参数使用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 00:24:01