简体   繁体   English

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

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

I am trying to convert an terraform variable written in HCL to a dynamically generated tf.json file containing the variable, but I am running into errors.我正在尝试将用 HCL 编写的 terraform 变量转换为动态生成的包含该变量的tf.json文件,但我遇到了错误。

HCL version I am trying to convert:我正在尝试转换的 HCL 版本:

variable "accounts" {
  type        = map(any)

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

I have tried the following format:我试过以下格式:

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

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

and

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

I get the following error:我收到以下错误:

│ 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.

Is there a tool that will convert HCL to valid .tf.json configurations?是否有工具可以将 HCL 转换为有效.tf.json配置? Or what am I missing on the formatting here?或者我在这里的格式上缺少什么?

Your specified type for the variable is a map(any) , so your default value for the variable must also be a map(any) , and cannot be a list(map(list(string))) .您为变量指定的类型是map(any) ,因此变量的默认值也必须是map(any) ,并且不能是list(map(list(string)))

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

That would assign a default value of type object(list(string)) which matches the same object(list(string)) type structure in your HCL2, and also would be a subset of the specified map(any) .这将分配一个object(list(string))类型的默认值,它与 HCL2 中相同的object(list(string))类型结构相匹配,并且也是指定map(any)的子集。

Your default value is a list of maps, but it should be only map:您的默认值是地图列表,但它应该仅为 map:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM