首页 > 解决方案 > 将 terraform HCL 变量 type=map(any) 转换为 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配置的工具?或者我在这里的格式缺少什么?

标签: jsonterraformhcl

解决方案


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

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

推荐阅读