liveBook · Manning

Terraform variable file

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A tfvars file is a Terraform variable-definition file containing values that can be loaded into a configuration.

A Terraform variable file stores input variable values separately from configuration, reducing repeated typing and making environment-specific deployments easier to reproduce. It is especially useful when interactive input would create errors or cannot support automation. section 5.4.1 section 5.4

HCL variable files use simple key = value expressions rather than blocks and may contain literal values of any supported data type, including objects and maps. JSON is also supported, particularly when another system generates the file. section 5.4.3

Load environment values

Store production values in production.tfvars, then run terraform plan -var-file=production.tfvars. Terraform also loads terraform.tfvars, terraform.tfvars.json, and matching *.auto.tfvars or *.auto.tfvars.json files automatically when they exist. section 5.4.3

Listing 5.6 Variable file with HCL
# Production Data                             #1
vpc           = "vpc-01234567890abcdef"       #2
num_instances = 2

An HCL variable-file example assigns a VPC identifier and instance count with readable comments. section 5.4.3

Listing 5.7 Variable file with JSON
{                                              #1
  "vpc": "vpc-01234567890abcdef",              #2
  "num_instances": 2                           #3
}

A JSON variable-file example represents the same kind of input values for generated or programmatically managed files. section 5.4.3

Choose the file form and loading method deliberately:

  • Use .tfvars for human-managed HCL and .tfvars.json for JSON; Terraform uses the extension to determine the format. section 5.4.3
  • Pass -var-file multiple times when combining files, or use automatic naming patterns for standard inputs. section 5.4.3
  • Treat sensitive values cautiously: variable files may enter version control, while CI/CD secret support or a secrets manager can keep Terraform from handling the secret directly. section 5.4.6

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines HCL and JSON formats, naming conventions, loading flags, and security implications without detailing Terraform evaluation internals.
Examples
Uses production.tfvars commands plus HCL and JSON assignments for VPC identifiers and instance counts.
What it assumes
Assumes familiarity with Terraform input variables and basic HCL or JSON, while defining variable-file syntax and naming rules.
What you can do after
Operate Terraform plans with repeatable environment values by selecting files explicitly or through automatic loading conventions.
How it connects
Connects root-level input variables with Terraform plans, environment variables, command-line input, automation, CI/CD systems, and secrets managers.

Also explained in

Compare how 2 books explain Terraform variable file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.