JSON to Terraform HCL

Convert JSON configuration objects to Terraform HCL format. Generate locals blocks, variable definitions, and resource configurations.

Input JSON
Terraform HCL

Features

đŸ—ī¸ Three Output Modes

  • Raw HCL key = value assignments
  • Wrapped in a locals block
  • Individual variable blocks
  • Automatic mode switching

📐 Aligned Formatting

  • Optional = sign alignment
  • Consistent indentation
  • Proper HCL block syntax
  • Nested object support

⚡ Type Handling

  • Strings double-quoted
  • Numbers and booleans raw
  • Arrays of primitives inline
  • Nested objects as HCL blocks

Terraform HCL Guide

HCL Syntax Basics

HCL (HashiCorp Configuration Language) uses unquoted keys and = for assignments. Objects become blocks with braces, and primitive arrays use square bracket syntax.

resource_group = "myapp-prod"
location       = "eastus"
vm_size        = "Standard_D2s_v3"

tags = {
  env  = "production"
  team = "platform"
}

Bringing JSON data into Terraform

Converting JSON to HCL is handy when moving settings from another tool into Terraform, or turning an exported configuration into variables. It helps to know when HCL conversion is the right move and when Terraform can read the JSON directly.

Tips and common pitfalls

  • Terraform can read JSON natively with jsondecode(file("config.json")), which avoids a conversion step for data that changes often.
  • Terraform also accepts whole configuration files written as .tf.json, useful when the files are generated by another program.
  • Put environment-specific values in .tfvars files and keep resources generic.
  • Run terraform fmt after pasting generated HCL to match the standard formatting.

More questions

How do I declare a variable for a nested object?

Use a typed object, for example type = object({ name = string, tags = map(string) }), so Terraform validates the structure before planning.

Should secrets go in tfvars files?

No. Pass secrets through environment variables (TF_VAR_name) or a secrets manager, and mark the variable sensitive = true.