Terraform tfvars to JSON Converter

Convert .tfvars to terraform.tfvars.json. Two things go wrong in a migration like this: the comments cannot come across, and both files are loaded if you leave both.

Paste below, or drop a file anywhere on this panel

Or drop a file anywhere on this panel. Nothing is uploaded: the analysis runs in this tab.

The answer appears here

Paste on the left and press Convert. Nothing leaves this tab.

Examples

Real input you can load into the tool above. Each one shows a different thing going wrong, because that is what the tool is for.

A file with a comment

The values convert and the comment cannot, so it is quoted back before it is lost

# Raised for TICKET-4821, do not lower without asking.
instance_count = 12
region = "eu-west-1"

A reference in a tfvars file

Already invalid: there is no expression language while a tfvars file is read

region = var.region

A credential

Converting changes the filename, and a .gitignore rule for *.tfvars will not match the new one

db_password = "REPLACE_ME"
region = "eu-west-1"

Common mistakes

These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.

  1. Leaving both terraform.tfvars and terraform.tfvars.json in place

    The JSON is loaded second, so it wins for every name it defines and the HCL still applies for the rest. Both files look right on their own.

    Instead:Delete the .tfvars file in the same commit that adds the .tfvars.json.

  2. Converting and losing the comments

    JSON has no comments, and a tfvars comment is usually the only record of why a value is what it is.

    Instead:Move the reasoning into the variable descriptions in variables.tf first, then convert.

  3. Forgetting the new filename in .gitignore

    A pattern for *.tfvars does not match *.tfvars.json, so a file that was ignored becomes committable.

    Instead:Add *.tfvars.json alongside *.tfvars.

terraform.tfvars.json is loaded after terraform.tfvars, so the JSON wins

Leaving both in place during a conversion means half the values come from each file, silently, with nothing to say which is which.

Delete the old file in the same commit

The JSON overrides the HCL for every name it defines and leaves the HCL in force for every name it does not. So a partially converted file produces a configuration nobody wrote, and both files look correct on their own.

Comments cannot come across, and they are usually the only record of why

JSON has no comment syntax. A comment in a tfvars file is nearly always the note explaining which ticket raised a limit, which account an id belongs to, or what breaks if it changes. Move that reasoning into the variable descriptions in variables.tf before converting, which is a better home for it anyway.

Only two filenames are loaded without being asked for

terraform.tfvars.json, and anything matching *.auto.tfvars.json. Any other name, prod.tfvars.json included, needs -var-file on every command. That is useful for per-environment files and a surprise for anybody who renamed the default file to something tidier.

A tfvars file of either kind holds literals only

There are no variables, no locals and no functions available while it is read. A reference in a tfvars file is already invalid rather than something the conversion loses, so this page refuses those lines rather than guessing at them.

A heredoc becomes one escaped string

Correct and unreadable: every newline becomes an escape on a single line, which ends any hope of reviewing an IAM policy or a startup script in a diff. An indented heredoc also had its indentation stripped during parsing, and that is not recoverable. Consider moving the document to its own file and reading it with file() instead.

Check .gitignore, because the filename changed

A rule for *.tfvars does not match *.tfvars.json. A file that was safely ignored becomes a file that is not, and the values in it did not get less sensitive by changing format.

More terraform tools

Terraform Validator & Formatter Does it parse, and is it formatted? Terraform dynamic Block Generator It is ingress.value, not each.value Terraform check and precondition Generator A failed check does not stop the apply Terraform variables.tf Generator From the var. references you already wrote Terraform Unused Declaration Linter Dead variables, locals and data sources Terraform Atlantis YAML Generator when_modified misses your modules Terraform Function Tester and HCL Console terraform console, in a tab Terraform for Expression Tester Where do the keys collide? Terraform Interpolation Tester null becomes an empty string Terraform templatefile Tester Render it before you apply it Terraform Resource Count Calculator How many objects, not how many blocks Terraform count to for_each Converter With the moved blocks it needs Terraform moved Block Generator Rename without destroying Terraform state mv Command Builder No plan, no undo Terraform target Flag Builder And why to stop using it Terraform outputs.tf Generator Outputs are not sensitive by default Terraform Remote State Data Source Generator It grants read of the whole state Terraform Provider Block Generator docker is not hashicorp/docker Terraform .gitignore Generator and Checker .terraform* eats the lock file Terraform to OpenTofu Migration Checker The state is the part to watch Terraform Version Constraint Parser What ~> actually allows Terraform Resource Address Parser Quote it before the shell eats it Terraform cidrsubnet Calculator newbits is added, not the target Terraform Import Block Generator The id is not the name Terraform Lock File Viewer Why CI fails and your laptop does not Terraform Variables Checker A missing value hangs CI Terraform tfvars to Env Vars Converter Lists and maps have to be JSON Terraform Deprecated Syntax Checker What a current Terraform rejects Terraform State File Viewer Read it without uploading it Terraform Plan JSON Viewer It is not redacted Terraform HCL to JSON Converter And what the conversion loses Terraform Module Source Validator version only works for the registry Terraform Backend Config Decoder Is state actually locked? Terraform Naming Convention Validator A hyphen makes a resource unreferenceable

Elsewhere on the site