Convert Terraform Variable Definitions to JSON
Convert Terraform variable blocks with types, defaults, descriptions, and validation rules from HCL to JSON format.
Detailed Explanation
Terraform Variables in HCL and JSON
Variables are the input parameters of a Terraform module. Each variable block defines a single input with optional type, default value, description, and validation rules.
HCL Variable Definitions
variable "region" {
description = "AWS region for resource deployment"
type = "string"
default = "us-east-1"
}
variable "instance_count" {
description = "Number of instances to launch"
type = "number"
default = 2
}
variable "enable_logging" {
description = "Enable CloudWatch logging"
type = "bool"
default = true
}
variable "allowed_cidrs" {
description = "CIDR blocks allowed to access the service"
type = "list(string)"
default = ["10.0.0.0/8", "172.16.0.0/12"]
}
JSON Structure
Each variable name becomes a key under the variable object:
{
"variable": {
"region": {
"description": "AWS region for resource deployment",
"type": "string",
"default": "us-east-1"
},
"instance_count": {
"description": "Number of instances to launch",
"type": "number",
"default": 2
}
}
}
Type Expressions
In HCL, type constraints like string, number, bool, list(string), and map(string) are expressions. In JSON, they are represented as strings. Complex types like object({name = string, age = number}) also become string representations.
Validation Rules
Variable validation blocks with condition and error_message attributes translate to nested objects. The condition expression becomes a string in JSON.
Use Case
Creating reusable Terraform modules with well-defined inputs, generating variable definition files from API specifications, or converting between HCL and JSON formats for Terraform Cloud workspace variable configuration.