Terraform variables.tf Generator

Write the variable blocks for everything a file refers to and does not declare. The types come from how each one is used, which is a guess, and the page says so on every one.

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 Generate. 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.

References with mixed usage

The count reference guesses a number and the rest guess string, each labelled as a guess

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = var.instance_type
  count         = var.instance_count
}

A credential reference

Marked sensitive, with the reminder that sensitive does nothing about state

resource "aws_db_instance" "main" {
  password = var.db_password
}

Common mistakes

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

  1. Declaring variables without types

    Everything becomes any, and a wrong shape is caught wherever it is finally used rather than where it was set, often several modules away.

    Instead:Give every variable a type. Narrow it as far as the real values allow.

  2. Trusting an inferred type

    Inference from usage cannot tell list(string) from set(string) or from a list of objects. The wrong one fails later and further away.

    Instead:Check each against what callers actually pass before committing the file.

  3. Adding a default to make a variable optional

    A default that is not right for every caller becomes the value nobody meant to choose, silently, in whichever environment forgot to set it.

    Instead:Leave it required and pass -input=false in automation so a missing value fails immediately.

A type is the cheapest validation Terraform has

It costs one line and it catches the wrong shape where the value is set, rather than several modules deeper where it is finally used.

The inferred type is a guess, not an analysis

list(string) is inferred from indexing or iteration, and the real type could be a set, a list of objects, or list(any). The wrong guess fails at the point the value is used rather than at the declaration, so each one is worth checking against what callers actually pass.

Without a type, everything is any

And a wrong value is not caught anywhere near where it was set. It surfaces wherever the value finally lands, often several modules down, as an error about a type that appears nowhere in the file the person is reading.

None of these gets a default, deliberately

A default is a decision about what happens when nobody chooses, and for most module inputs the honest answer is that there is no sensible default. Remember that a required variable with no value PROMPTS rather than failing, so automation needs -input=false or it hangs.

A validation block catches what a type cannot

A region is a string and only some strings are regions. Without a validation block, a typo surfaces as a provider error naming an endpoint nobody recognises. With one, it fails at the variable with a message you wrote.

A description is published whether or not you write one

terraform-docs renders the table from these, so an empty description produces a blank column in the module's documentation. Say what the value is for rather than restating the name.

sensitive does less than it appears to

It stops Terraform printing the value in plan and apply output. It does nothing about state, where the resolved value is written in plaintext, and nothing about the tfvars file it might be set in. Reading a credential from a secret manager with a data source keeps it out of the variable entirely.

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 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 tfvars to JSON Converter The JSON file wins 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