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

Terraform加载多IP列表至Akamai JSON配置报错求助

解决Akamai Terraform模块加载多IP列表时的JSON格式错误

问题场景

项目结构如下:

  • 主目录:main.tf
  • firewall-ip-list/:存放iplist1.txt、iplist2.txt两个IP列表文件
  • assets/:包含config.json配置文件

使用akamai_property_rules_template模块加载JSON配置时,单个IP列表能正常执行terraform plan和apply,但给JSON的values数组添加第二个IP列表后,触发错误:

invalid JSON result: invalid character '1' after array element

解决步骤

1. 检查JSON数组的语法格式

最常见的问题是数组元素之间漏加逗号,或者IP列表文件内容破坏了JSON结构。

  • 错误示例(漏逗号):
    "values": [
      "${file("firewall-ip-list/iplist1.txt")}"
      "${file("firewall-ip-list/iplist2.txt")}"
    ]
    
  • 修正后的写法:
    "values": [
      "${file("firewall-ip-list/iplist1.txt")}",
      "${file("firewall-ip-list/iplist2.txt")}"
    ]
    

同时检查两个IP列表文件:确保每行一个IP,文件末尾不要留多余空行或特殊字符(比如逗号、引号),避免替换后JSON结构出错。

2. 手动验证IP列表内容

用terraform console测试文件读取结果,确认内容合法:

terraform console
> file("firewall-ip-list/iplist1.txt")
> file("firewall-ip-list/iplist2.txt")

如果输出包含换行符或非IP字符,先清理IP列表文件。

3. 用jsonencode确保数组格式正确

如果直接在JSON模板里嵌入file()函数容易出拼接问题,换用Terraform变量先读取所有IP列表,再生成合法JSON数组:
在main.tf中添加变量定义:

variable "ip_lists" {
  type = list(string)
  default = [
    file("firewall-ip-list/iplist1.txt"),
    file("firewall-ip-list/iplist2.txt")
  ]
}

module "akamai_property" {
  source = "akamai/property-rules-template/akamai"
  # 其他必要配置(比如property_id、rule_format等)
  template_file = "assets/config.json"
  template_vars = {
    ip_values = jsonencode(var.ip_lists)
  }
}

然后修改assets/config.json的values部分:

"values": ${ip_values}

4. 升级模块到最新版本

旧版akamai_property_rules_template可能对多元素数组的模板替换支持不完善,升级到最新稳定版本试试:

module "akamai_property" {
  source  = "akamai/property-rules-template/akamai"
  version = "~> 3.0" # 替换为实际最新版本号
  # 其他配置...
}

内容的提问来源于stack exchange,提问作者Daniel dos Anjos Souza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 21:57:20