JSON / YAML → Terraform HCL
Terraform HCL Infrastructure Engineering

JSON / YAML to Terraform HCL Variable & Locals Generator

Convert JSON and YAML payloads into Terraform HCL variable type constraints (type = object({...})), default values, and terraform.tfvars definitions.

Zero Server Transmission
Terraform 1.3+ optional()
Presets:
Input Payload (JSON / YAML)yaml
variable "app_config" {
  description = "Auto-generated Terraform variable constraint for app_config"
  type = object({
    cidr_block           = string
    enable_dns_hostnames = bool
    enable_dns_support   = bool
    max_azs              = number
    nat_gateways         = list(string)
    subnets              = list(object({
      az             = string
      cidr           = string
      is_public      = bool
      name           = string
      route_table_id = string
    }))
    tags                 = map(string)
    vpc_name             = string
  })
  default = {
    cidr_block           = "10.100.0.0/16"
    enable_dns_hostnames = true
    enable_dns_support   = true
    max_azs              = 3
    nat_gateways         = ["nat-08127394821a", "nat-09823412344b"]
    subnets              = [
      {
        az             = "us-east-1a"
        cidr           = "10.100.1.0/24"
        is_public      = true
        name           = "public-1a"
        route_table_id = "rtb-01a9b2c3d4"
      },
      {
        az             = "us-east-1b"
        cidr           = "10.100.2.0/24"
        is_public      = true
        name           = "public-1b"
        route_table_id = "rtb-01a9b2c3d4"
      },
      {
        az             = "us-east-1a"
        cidr           = "10.100.10.0/24"
        is_public      = false
        name           = "private-app-1a"
        route_table_id = "rtb-09f8e7d6c5"
      },
      {
        az             = "us-east-1b"
        cidr           = "10.100.11.0/24"
        is_public      = false
        name           = "private-app-1b"
        route_table_id = "rtb-09f8e7d6c5"
      }
    ]
    tags                 = {
      CostCenter  = "infra-0091"
      Environment = "production"
      ManagedBy   = "terraform"
      Owner       = "cloud-platform-team"
    }
    vpc_name             = "prod-useast1-vpc"
  }
}
Schema Complexity
13 Inferred FieldsDepth 2
Optional Attributes
0 Optionals Detectedv1.3+ Enabled
Export Dialects
4 Output Formats.tf / .tfvars / .json

100% Client-Side AST Synthesis

All JSON and YAML parsing, AST tree traversal, and Terraform HCL type synthesis execute locally in your browser memory with zero network requests or telemetry.

Terraform 1.3+ Conformance & Optionals

Automatically detects missing or nullable fields and wraps them in standard optional(type, default) syntax, preventing Terraform plan-time runtime type errors.

Multi-Dialect Infrastructure Export

Seamlessly exports production-ready variables.tf type constraints, terraform.tfvars value definitions, locals.tf configuration blocks, and tfvars.json files.

Technical Reference & Architecture Guide

JSON / YAML to Terraform HCL Variable & Locals Generator — Reference Guide

Convert JSON and YAML payloads into Terraform HCL variable type constraints (type = object({...})), default values, locals blocks, and terraform.tfvars definitions.

Key Parameters & Controls

source_formatstring

Specifies the parser for the source configuration payload ('json' or 'yaml').

Default: "json"
target_modestring

Determines the target HCL output structure: 'variable' (with explicit type constraint and default), 'locals' (computed expression), or 'tfvars' (raw variable assignment).

Default: "variable"
variable_namestring

The identifier assigned to the generated HCL variable block or locals attribute key.

Default: "app_config"
use_optional_attributesboolean

Enables Terraform 1.3+ optional() attribute syntax with fallback defaults for nullable or omitted object attributes.

Default: true
indent_spacesnumber

Number of spaces applied for indentation in formatted HCL code blocks.

Default: 2

Pro Tips & Best Practices

When defining complex nested object

When defining complex nested object schemas, wrap non-mandatory keys with optional(type, default_value). This prevents null-reference runtime errors and eliminates cumbersome coalesce() lookups inside child modules.

Standard JSON lists with uniform

Standard JSON lists with uniform item types convert cleanly to list(string) or list(number). If your array mixes disparate types or object structures, convert to tuple([type1, type2]) instead of falling back to list(any).

If your orchestration pipeline generates

If your orchestration pipeline generates dynamic JSON payloads at deploy time, consider saving them directly as terraform.auto.tfvars.json instead of generating raw HCL. Terraform reads native JSON variables automatically.

HCL identifiers must match

HCL identifiers must match ^[a-zA-Z_][a-zA-Z0-9_-]*$. If your JSON/YAML keys contain hyphens, colons, or dots (common in Kubernetes or AWS tags), wrap the object map keys in quotes ("tag:environment" = string) to avoid syntax errors.

Common Gotchas & Troubleshooting

Error: Invalid attribute name - Hyphens in unquoted HCL identifiers

Unquoted identifiers with hyphens are treated as minus arithmetic expressions in HCL object bodies. Ensure object keys containing hyphens are enclosed in double quotes (e.g., "service-mesh" = object({...})).

Type mismatch when passing null values to non-nullable attributes

Terraform defaults object attributes to non-nullable. Enable the optional modifier in your type schema: type = object({ key = optional(string) }) or explicitly define nullable = true on the top-level variable block.

Malformed multiline YAML scalar causing parser failure

Ensure YAML block scalar indicators (| or >) maintain uniform 2-space indentation on following lines and contain no raw tab (\t) characters, which violate the YAML specification.

Frequently Asked Questions

How does this generator infer Terraform HCL types from JSON/YAML values?▼

Strings become string, numbers become number, and booleans become bool. Arrays with identical child schemas map to list(type), arrays with varying schemas map to tuple([...]), key-value dictionaries map to map(type) when values are uniform, or strictly typed object({...}) blocks when keys are distinct.

What is the difference between generating a variable block versus a locals block?▼

A variable block declares an input contract containing a type constraint, description, and default value, allowing external callers to override it. A locals block defines an internal, immutable expression evaluated within the module scope without requiring external inputs.

Why should I use explicit type = object({...}) instead of type = any?▼

Using type = any skips compile-time schema validation, postponing errors to runtime execution or remote state operations. Explicit object schemas provide early linting during terraform validate, enable IDE autocompletion, and document module requirements.

Can I use the generated HCL directly in Terraform Cloud or Terragrunt?▼

Yes. Variable definitions can be placed directly in variables.tf, defaults can populate terraform.tfvars or terragrunt.hcl inputs blocks, and locals can be referenced across your Terraform root and child modules.

Related Tools & Utilities

All Tools →