将 terraform HCL 变量 type=map(any) 转换为 JSON

convert terraform HCL variable type=map(any) to JSON

我正在尝试将用 HCL 编写的 terraform 变量转换为包含该变量的动态生成的 tf.json 文件,但我 运行 出错了。

我正在尝试转换的 HCL 版本:

variable "accounts" {
  type        = map(any)

  default = {
    acct1     = ["000000000001"]
    acct2     = ["000000000002"]
  }
}

我试过以下格式:

{
  "variable": {
    "accounts": {
      "type": "map(any)",

      "default": [
        { "acct1": "000000000001" },
        { "acct2": "000000000002"}
      ]
    }
  }
}

{
  "variable": {
    "accounts": {
      "type": "map(any)",
      "default": [
        {
          "acct1": ["000000000001"],
          "acct2": ["000000000002"]
        }
      ]
    }
  }
}

我收到以下错误:

│ Error: Invalid default value for variable
│ 
│   on accounts.tf.json line 6, in variable.accounts:
│    6:       "default": [
This default value is not compatible with the variable's type constraint: map of any single type required.

是否有工具可以将 HCL 转换为有效的 .tf.json 配置?或者我在此处的格式上遗漏了什么?

您指定的变量类型是 map(any),因此您的变量默认值也必须是 map(any),不能是 list(map(list(string)))

{
  "variable": {
    "accounts": {
      "type": "map(any)",
      "default": {
        "acct1": ["000000000001"],
        "acct2": ["000000000002"]
      }
    }
  }
}

这将分配类型 object(list(string)) 的默认值,它与 HCL2 中相同的 object(list(string)) 类型结构相匹配,并且也是指定 map(any) 的子集。

您的默认值是地图列表,但应该只有地图:

      "default": {
         "acct1": "000000000001",
         "acct2": "000000000002"
      }