Problem
During the plan phase of a run in an HCP Terraform or Terraform Enterprise workspace, you may encounter the following error:
Error: No value for required variable
on XXX.tf line XX:
variable "your_variable_name_here" {
The root module input variable "your_variable_name_here" is not set, and has no default value. Use a -var or -var-file command line argument to provide a value for this variable.Cause
This error occurs because a variable declared in your Terraform configuration does not have a value assigned to it. Terraform requires that all declared variables without a default value receive a value at runtime.
In HCP Terraform and Terraform Enterprise, values can be provided through workspace variables (Terraform or environment variables), or within the configuration files themselves.
Solutions
This issue can be resolved through several methods, depending on your intended configuration.
Solution 1: Define a Default Value in Configuration
If the variable should have a default value, you can add it directly to the variable block in your .tf file. This is useful for values that are consistent across most deployments.
Update your configuration to include a default argument.
variable "your_variable_name_here" {
type = string
default = "your-default-value"
}Solution 2: Set a Terraform Variable in the Workspace
For values that change between environments, you should set them as workspace variables. Ensure the variable is set as a Terraform Variable, not an Environment Variable.
- Navigate to your workspace in the HCP Terraform or Terraform Enterprise UI.
- Go to the Variables tab.
- Under the Terraform Variables section, add a new variable.
- Set the Key to
your_variable_name_hereand provide the desired Value. - Save the variable.
Solution 3: Use an Environment Variable in the Workspace
If you must use an environment variable, Terraform requires it to be prefixed with TF_VAR_.
- Navigate to your workspace's Variables tab.
- Under the Environment Variables section, find or create your variable.
- Set the Key to
TF_VAR_your_variable_name_here. - Provide the desired Value and save the variable.
Solution 4: Resolve Conflicting Variable Names (Terraform Enterprise)
In Terraform Enterprise v202111-1 and newer, having a Terraform Variable and an Environment Variable with the same effective name (e.g., my_var and TF_VAR_my_var) can cause conflicts. The workaround is to ensure the variable exists in only one category or to rename one of them to avoid the conflict.
Solution 5: Set the Terraform Working Directory
If your configuration is in a subdirectory of your version control repository (a monorepo), you must specify the path to that directory in your workspace settings. If the working directory is not set, HCP Terraform may not load the correct .tfvars files or recognize the intended root module, leading to this error.
- Navigate to your workspace in the UI.
- Go to Settings > General.
- In the Terraform Working Directory field, enter the path to the directory containing your root Terraform configuration (e.g.,
terraform/production). - Save the settings.