【问题标题】:convert terraform HCL variable type=map(any) to JSON将 terraform HCL 变量 type=map(any) 转换为 JSON
【发布时间】:2021-11-25 05:30:20
【问题描述】:

我正在尝试将用 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 配置?或者我在这里的格式缺少什么?

【问题讨论】:

    标签: json terraform hcl


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您为变量指定的类型是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) 的子集。

      【讨论】:

        猜你喜欢
        • 2021-08-10
        • 2020-11-21
        • 1970-01-01
        • 2021-02-28
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多