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

Time:10-05

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 version I am trying to convert:

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? Or what am I missing on the formatting here?

CodePudding user response:

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

{
  "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).

CodePudding user response:

Your default value is a list of maps, but it should be only map:

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