What is YAML to Terraform Variable Generator?
Terraform uses HCL (HashiCorp Configuration Language) to declare infrastructure variables, but many teams already store environment configurations in YAML because it is concise and human-friendly. This tool bridges that gap by converting any YAML mapping into two Terraform-ready artifacts: a variables.tf file containing fully typed variable blocks with optional descriptions and default values, and a terraform.tfvars file with simple key = value assignments. The converter inspects every value in your YAML and infers the correct HCL type — strings become string, integers and floats become number, true and false become bool, homogeneous arrays become list(element_type), and nested objects become object({...}) with recursively typed attributes. You can also add a variable prefix to namespace every name, which is useful when multiple modules share similar keys like region or name.
How to Use
- Paste your YAML configuration — typically an environment or infrastructure config — into the input area on the left.
- Toggle the 'Include descriptions' checkbox if you want auto-generated description fields in each variable block.
- Optionally enter a variable prefix (e.g., app) to produce namespaced variables like app_location instead of location.
- Click 'Convert' and the tool will parse your YAML, infer HCL types, and generate both outputs simultaneously.
- Switch between the variables.tf and terraform.tfvars tabs to view each file, then copy the content into your Terraform project.
Why Use This Tool?
Tips & Best Practices
- Use the variable prefix to match your module name — for example, prefix db_ for a database module keeps variables organized in large projects.
- The type inference examines the first element of each array to determine the list element type; make sure your sample data is representative.
- Review and customize the auto-generated descriptions before committing — they are helpful starting points but may need domain-specific context.
- For sensitive values like passwords or API keys, consider removing the default from the variable block and marking it sensitive = true after conversion.
- Complex nested objects produce deeply nested object({...}) types; if these become unwieldy, consider flattening the YAML structure before converting.
Frequently Asked Questions
How does the tool map YAML types to Terraform HCL types?
YAML strings map to string, integers and floats map to number, booleans map to bool, arrays of uniform type map to list(element_type), arrays of mixed types map to list(any), and nested objects map to object({...}) with each attribute recursively typed. Null values default to the any type since their intended type cannot be inferred.
When should I NOT use this converter?
This tool is designed for flat or moderately nested YAML configuration maps. It is not suitable for converting full Terraform resource definitions, modules, or data sources — those require HCL-specific syntax like resource blocks, provider configurations, and dynamic blocks that have no YAML equivalent. For those, write HCL directly or use tools like tfjson.
What is the difference between variables.tf and terraform.tfvars?
variables.tf declares variable blocks with type constraints, descriptions, and default values — it defines the interface. terraform.tfvars assigns concrete values to those variables — it provides the data. You commit variables.tf to version control and typically keep terraform.tfvars out of it or use .tfvars files per environment.
Can I convert YAML arrays at the top level?
No. The converter expects a YAML mapping (object) at the root because Terraform variable blocks are key-value structures. If your YAML is a top-level array, wrap it in an object like { items: [...] } before converting.
Does the variable prefix affect both outputs?
Yes. The prefix is prepended to every key with an underscore separator in both variables.tf and terraform.tfvars, so variable names stay consistent across the two files. For example, a prefix of app turns region into app_region in both outputs.
Is my infrastructure data sent to a server?
No. All parsing and conversion runs entirely in your browser. Your YAML configuration and the generated Terraform code never leave your device. No data is collected, stored, or transmitted to any server.
Real-world Examples
Multi-Environment AWS Infrastructure Config
Convert a production AWS infrastructure YAML into typed Terraform variables with a namespace prefix.
resource_group: myapp-prod location: us-east-1 instance_type: t3.medium replicas: 3 enabled: true tags: env: production team: platform network: vpc_cidr: "10.0.0.0/16" public_ip: false
variable "prod_resource_group" {
type = string
description = "resource_group configuration value"
default = "myapp-prod"
}
variable "prod_location" {
type = string
description = "Cloud region or location"
default = "us-east-1"
}
variable "prod_instance_type" {
type = string
description = "instance_type configuration value"
default = "t3.medium"
}
variable "prod_replicas" {
type = number
description = "Number of instances or replicas"
default = 3
}
variable "prod_enabled" {
type = bool
description = "Enable or disable "
default = true
}
variable "prod_tags" {
type = object({
env = string
team = string
})
description = "tags configuration object"
default = {
env = "production"
team = "platform"
}
}
variable "prod_network" {
type = object({
vpc_cidr = string
public_ip = bool
})
description = "network configuration object"
default = {
vpc_cidr = "10.0.0.0/16"
public_ip = false
}
}terraform.tfvars for the Same Config
The same YAML generates simple key = value assignments for terraform.tfvars, ready to use per-environment.
resource_group: myapp-prod location: us-east-1 replicas: 3 enabled: true
prod_resource_group = "myapp-prod" prod_location = "us-east-1" prod_replicas = 3 prod_enabled = true