Terraform Variable Generator
Build Terraform variable blocks and terraform.tfvars files with a visual form. Supports complex types, validation rules, and common templates.
About This Tool
The Terraform Variable Generator is a free browser-based tool that helps
you create well-structured Terraform variable definitions (variables.tf)
and matching terraform.tfvars files without writing HCL by hand. Define
each variable's name, type, description, default value, validation rules,
and sensitive flag through a simple form, and the tool generates the correct
Terraform syntax instantly.
The generator supports all common Terraform types — string, number,
bool, list(string), list(number), map(string), map(number),
set(string), object({...}), and any. For complex object types you
can specify nested field definitions. Validation blocks with custom
conditions and error messages are supported, letting you enforce constraints
like valid AWS regions, allowed instance types, or CIDR block formats.
To get started quickly, the tool includes common variable templates for AWS regions, EC2 instance types, VPC CIDRs, subnet lists, database credentials, tags, environments, and project names. Click a template to populate the form, then customize as needed. If you work with Terraform HCL files directly, the HCL to JSON Converter can help you transform between formats. For formatting YAML-based config files that accompany your infrastructure, try the YAML Formatter. And to validate the JSON configuration your modules consume, the JSON Formatter is always handy.
All processing runs entirely in your browser. No data — including sensitive variable values — is ever sent to any server.
How to Use
- Click a Quick Template button (e.g., "AWS Region", "Common Tags") to pre-fill a variable, or click Add Variable to start from scratch.
- Enter the Variable Name using Terraform naming conventions (lowercase, underscores).
- Select the Type from the dropdown — choose from string, number, bool, list, map, set, object, or any.
- Add a Description explaining what the variable controls.
- Set a Default Value in valid HCL syntax (e.g.,
"us-east-1",42,["a", "b"]). - Toggle Sensitive if the variable contains secrets (passwords, API keys). Toggle Nullable if the variable can accept
null. - Optionally add a Validation condition and error message to enforce constraints. Use Terraform expression syntax (e.g.,
length(var.name) > 0). - Review the generated variables.tf and terraform.tfvars in the output panels below. Click Copy or Download to use them in your project.
Popular Terraform Variable Examples
FAQ
What Terraform types are supported?
The tool supports string, number, bool, list(string), list(number), map(string), map(number), set(string), object({...}), and any. For object types, you can specify nested field definitions in the Object Fields textarea using HCL syntax like { name = string, age = number }.
How do validation rules work?
Validation rules use Terraform expression syntax in the condition field. The condition must evaluate to true for the variable value to be accepted. Common patterns include length() checks, contains() for allowed values, can(regex()) for pattern matching, and can(cidrhost()) for CIDR validation. The error message is shown when validation fails during terraform plan or apply.
What are sensitive variables?
Marking a variable as sensitive tells Terraform to redact its value from plan output, state output, and logs. Use this for passwords, API keys, tokens, and other secrets. The generated variable block includes sensitive = true. Note that the value is still stored in state, so you should also encrypt your state backend.
Can I generate variables for modules?
Yes. The generated variables.tf output works for both root modules and child modules. For child modules, you typically define variables without default values so that the calling module must provide them. Simply leave the Default Value field empty for required variables.
How should I format default values?
Default values must use valid HCL syntax. Strings need quotes: "us-east-1". Numbers are bare: 42. Booleans are true or false. Lists use brackets: ["a", "b"]. Maps use braces: { key1 = "value1", key2 = "value2" }. Object defaults follow the same syntax as maps but with typed fields.
Is my data safe?
Yes. All processing runs entirely in your browser using JavaScript. No variable names, default values, or sensitive flags are ever sent to any server. You can verify this by checking the Network tab in your browser's developer tools.
Can I use this for Terraform Cloud or OpenTofu?
Yes. The generated HCL syntax is compatible with Terraform 0.13+, Terraform Cloud, Terraform Enterprise, and OpenTofu. Variable definitions and tfvars files follow the standard HashiCorp Configuration Language specification.
Related Tools
Terraform HCL Formatter
Format, validate, and beautify Terraform HCL code with auto-indent, equals alignment, attribute sorting, and syntax highlighting.
HCL ↔ JSON Converter
Convert between Terraform HCL and JSON formats with syntax highlighting and tree view.
AWS IAM Policy Generator
Generate AWS IAM policy JSON documents from a visual form. Select services, actions, resources, and conditions with common policy templates.
YAML Formatter
Format, validate, and minify YAML with customizable indentation and syntax error display.
JSON Formatter
Format, validate, and beautify JSON with syntax highlighting and tree view.