How to fix Error: Required variable not set in Terraform
This error occurs when a Terraform configuration requires an input variable but no value is provided for it. Terraform needs variable values supplied via -var flags, .tfvars files, environment variables, or default values in the variable declaration.
Quick fix
Fixes Error: Required variable not set
terraform plan -var="variable_name=value"
terraform apply -var="aws_region=us-east-1"- Provide the variable using the -var flag
- Create or use a terraform.tfvars file
- Use environment variables with TF_VAR_ prefix
- Add default values to variable declarations
terraform plan -var="variable_name=value"terraform apply -var="aws_region=us-east-1"Error: Required variable not set
What this error means
The 'Required variable not set' error means that your Terraform configuration declares one or more input variables without default values, but you haven't provided values for them when running terraform plan or terraform apply. Terraform requires all variables to have either a default value in the variable block or a value passed at runtime.
Typical symptoms
- terraform plan or terraform apply fails immediately with 'Error: Required variable not set'
- Error message specifies which variable is missing (e.g., 'Required variable not set: aws_region')
- Commands work in one environment but fail in another due to missing variable assignments
- Scripts or CI/CD pipelines fail because environment-specific variables aren't being loaded
Common causes
- Variable is declared without a default value and no -var or -var-file flag is used
- terraform.tfvars file exists but is not being loaded (file not named correctly or -var-file flag missing)
- TF_VAR_* environment variable is not set for a required variable
- Working with modules where child module variables aren't passed from the root module
- Variable is defined in one .tfvars file but a different file is being loaded
- Recently refactored code removed a variable assignment but left the declaration
How to fix "Error: Required variable not set"
Run terraform plan or apply with the -var flag to supply the variable value directly:
terraform plan -var="variable_name=value"
terraform apply -var="aws_region=us-east-1"For multiple variables, use multiple -var flags:
terraform plan -var="region=us-west-2" -var="environment=prod"Create a file named terraform.tfvars (or terraform.tfvars.json) in your working directory with variable assignments:
# terraform.tfvars
aws_region = "us-east-1"
environment = "production"
access_key = "your-key-here"Terraform automatically loads this file. To use a custom filename, include the -var-file flag:
terraform plan -var-file="prod.tfvars"Set environment variables in the format TF_VAR_<variable_name> before running Terraform:
export TF_VAR_aws_region="us-east-1"
export TF_VAR_environment="production"
terraform planOn Windows, use set instead of export:
set TF_VAR_aws_region=us-east-1
terraform planIf the variable should have a sensible default, add it to the variable block in your variables.tf file:
variable "aws_region" {
type = string
default = "us-east-1"
}
variable "environment" {
type = string
default = "dev"
}With defaults, the variable becomes optional at runtime.
Ensure all variables you're trying to use are declared in a variables.tf or main.tf file:
variable "required_var" {
type = string
description = "This variable is required"
# No default value means it must be supplied
}If the variable block doesn't exist, create it before trying to set its value.
If using Terraform Cloud or Terraform Enterprise, variables must be set in the workspace's Variables section:
1. Go to your workspace in the UI
2. Click Variables
3. Add the variable name (e.g., 'aws_region') with its value
4. Select 'Terraform variables' (not 'Environment variables') category
5. Save and rerun your plan/apply
Also verify that the workspace's Working Directory is set correctly under Settings > General.
Advanced notes
Variable precedence in Terraform (highest to lowest): -var command-line flags > .tfvars files > TF_VAR_* environment variables > default values. This means a -var flag will override values in .tfvars files. When working with modules, remember that parent modules must explicitly pass variable values to child modules via the module block. For CI/CD pipelines, using .auto.tfvars files (which are automatically loaded) or environment variables is often more reliable than command-line flags since they're easier to manage across different stages. In Terraform Cloud, use sensitive variables for secrets and store them securely in the workspace variables section rather than in version control.
Frequently asked questions
- What does "Error: Required variable not set" mean?
- The 'Required variable not set' error means that your Terraform configuration declares one or more input variables without default values, but you haven't provided values for them when running terraform plan or terraform apply. Terraform requires all variables to have either a default value in the variable block or a value passed at runtime.
- How do I fix "Error: Required variable not set"?
- This error occurs when a Terraform configuration requires an input variable but no value is provided for it. Terraform needs variable values supplied via -var flags, .tfvars files, environment variables, or default values in the variable declaration.
- What causes "Error: Required variable not set"?
- Variable is declared without a default value and no -var or -var-file flag is used terraform.tfvars file exists but is not being loaded (file not named correctly or -var-file flag missing) TF_VAR_* environment variable is not set for a required variable
- Is the "Error: Required variable not set" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.