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.