hcl2json
hcl2json copied to clipboard
Terraform fails for depends_on and list("string") cases
Hello. hcl2json works nice, but there are some cases that after conversion don't work.
Here you can see a list of fixtures used for different converter. Passing of those tests almost garanties that json file will be executable and valid for terraform.
Terraform init/plan fails for at least two cases. depens_on
(depends_on.hcl) case and type
case.
Depends_on case has to be without interpolation.
In case of type
- interpolation don't has to present as well. type
is reserved key for variables.
Example :
variable "availability_zones" {
type = list(string)
default = ["az1", "az2", "az3"]
}
Thank you.
I believe this is hitting a known limitation of hcl2json. From the README:
Note, however, that there are some corner cases where it may not be exactly equivalent, especially if the target application makes use of static analysis.
Both depends_on and type use static analysis in terraform.
Solving this would require hcl2json to know the schema of the target application. I'm not opposed to having some way to supply a schema to hcl2json to handle this case, but I don't currently have the time to design and implement this. Pull requests welcome though :). Having a schema available would also make going in the revers direction (json to hcl) possible.
My biggest question is, what would the schema file look like? Right now, schemas are typically defined exclusively using go code in the application itself, but for this use case it would be better to have a separate file (json, or maybe hcl?) that specifies the schema, preferable in a format that is general enough it could be used by other tools, not just hcl2json.
One way this could be solved is for hcl2json to support the same spec format as hcldec. See https://github.com/hashicorp/hcl/blob/main/cmd/hcldec/spec-format.md