Skip to content

Transforming a .tf file into .tf.json returns wrong types #179

Closed

Description

@Rami-Kassouf-FOO

Here's a minimal reproduction code to show what i mean

variables.tf

variable "create_main_node_group" {
  description = "Create main node group, main-node-group.tf"
  type        = bool
  default     = true
}

test.py

import json
from pathlib import Path
import hcl2

file_path = Path("./variables_override.tf").resolve()
with open(file_path) as fp:
    json_object = hcl2.load(fp)

file_path = file_path.with_suffix('.tf.json')
with open(file_path, 'w') as fp:
    json.dump(json_object, fp, indent=4)

expected output:

{
    "variable": [
        {
            "create_main_node_group": {
                "description": "Create main node group, main-node-group.tf",
                "type": "bool",
                "default": true
            }
        }]
}

actual output

{
    "variable": [
        {
            "create_main_node_group": {
                "description": "Create main node group, main-node-group.tf",
                "type": "${bool}",
                "default": true
            }
        }]
}

This breaks all terraform commands from working as it doesn't understand the new type:
terraform init

 Error: Invalid type specification

   on variables_override.tf.json line 6, in variable[0].create_main_node_group:
    6:                 "type": "${bool}",

A type specification is either a primitive type keyword (bool, number,
 string) or a complex type constructor call, like list(string).

There is also the issue that the variables is a list and all in all the transformation doesn't seem to work with this library which is a shame but this main issue is still a problem nontheless

Activity

  1. added
    bug
    on Jan 20, 2025
  2. added a commit that references this issue on Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions