Terraform Variables Generator

Builds a variables.tf file from repeatable rows, each with a name, HCL type (string/number/bool/list/map/object), default value, description, a sensitive toggle, and an optional validation block with a condition expression and error message, producing correctly formatted variable declarations. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-26

Overview

Introduction

A variables.tf file with several variables, each potentially needing a type, default, description, sensitivity flag, and validation rule, has a lot of small formatting details to get right by hand: correct type syntax, correct quoting for defaults, correct validation block nesting.

This tool generates that file from repeatable rows of variable settings, handling type-appropriate default formatting and validation block syntax automatically.

What Is Terraform Variables Generator?

A variables.tf generator supporting Terraform's six variable types (string, number, bool, list, map, object), optional defaults, descriptions, the `sensitive` flag, and an optional `validation` block with a condition expression and error message per variable.

Each row you add becomes one complete `variable "name" { }` block, and all blocks are concatenated in the order you added them.

How Terraform Variables Generator Works

For each variable row, you set a name, pick a type from a toggle group, optionally provide a default value, a description, toggle `sensitive`, and optionally fill in a validation condition and error message.

The tool formats the default value according to the chosen type (quoting strings, leaving other types as raw HCL), only emits a `sensitive` line when toggled on, and only emits a `validation` block when a condition was entered, keeping the output as close to hand-written HCL as possible.

When To Use Terraform Variables Generator

Use it when scaffolding a new module's variables.tf and you want consistent formatting across several variables without hand-aligning `=` signs and type syntax.

It's also useful for quickly adding a validation rule to an existing variable, since remembering the exact `condition`/`error_message` block nesting from memory is easy to get slightly wrong.

Features

Advantages

  • Handles type-appropriate default value formatting automatically (quoted strings vs. raw literals for other types).
  • Supports the `sensitive` flag and `validation` blocks, not just the basic name/type/default/description most generators stop at.
  • Catches duplicate variable names and invalid identifiers before you paste the output anywhere.

Limitations

  • List, map, and object default values must be typed as valid HCL literals yourself (e.g. `["a", "b"]`), the tool doesn't validate their internal syntax.
  • Only one validation rule per variable is supported; Terraform allows multiple `validation` blocks per variable for more complex rule sets.

Examples

A required, validated instance_count variable

Input

name: instance_count, type: number, default: (blank), description: "Number of instances to launch", sensitive: false, validation condition: var.instance_count > 0, error message: "instance_count must be greater than 0."

Output

variable "instance_count" {
  type        = number
  description = "Number of instances to launch"

  validation {
    condition     = var.instance_count > 0
    error_message = "instance_count must be greater than 0."
  }
}

Leaving the default blank makes the variable required (no `default` line), and the validation block only appears because a condition was provided.

Best Practices & Notes

Best Practices

  • Write descriptions that explain the variable's purpose, not just restate its name, since `terraform-docs` and `terraform plan -var` prompts surface that description text directly.
  • Mark any variable holding a credential, key, or token `sensitive`, but remember this only redacts CLI output, it doesn't encrypt the state file.
  • Add a validation rule for any variable whose valid values aren't already constrained by its type, e.g. an enum-like string or a positive-only number.

Developer Notes

Type formatting is driven by a small lookup: `string` values are quoted and backslash-escaped, while `number`/`bool`/`list`/`map`/`object` defaults are emitted verbatim since Terraform requires those as literal HCL expressions rather than quoted strings; the validation block is only rendered when a non-empty condition is supplied, falling back to a generic "Invalid value." error message if none was typed.

Terraform Variables Generator Use Cases

  • Scaffolding variables.tf for a new Terraform module
  • Adding a validation rule to an existing variable without re-deriving the block syntax from documentation
  • Standardizing variable descriptions and sensitivity flags across a team's modules

Common Mistakes

  • Typing a default value for a list/map/object type without valid HCL literal syntax (e.g. forgetting the surrounding brackets or braces).
  • Assuming `sensitive = true` encrypts the value in the state file, when it only redacts CLI/plan output.

Tips

  • Use the Terraform Outputs Generator right after to declare the outputs.tf entries these variables eventually feed into.

References

Frequently Asked Questions