How to fix error value for undeclared variable a variable named was assigned on the command line?



In this article we are going to fix

Error Value for undeclared variable A variable named was assigned on the command line, but the root module does not declare a variable of that name. To use this value, add a variable block to the configuration

The above mentioned issue very common with terraform and 99% of the case you might have forgotten -

  1. Either declaring variable
  2. Or did not create a variable.tf file.

Here is the pic which might help you to understand this issue visually -

How to fix?

  1. Look for the variable which you are trying to create.
  2. Create that variable inside your terraform file.
  3. Check your variable.tf and see if the variable is defined or not.
  4. Also check if there is mismatch in the variable name.

Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?