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.
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.
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.
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.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform