-var-file=filename

Hi. I am doing some practice and I am getting an error when using this “test” variable file terraform plan -var-file=test.tfvars

│ Error: Too many command line arguments

│ To specify a working directory for the plan, use the global -chdir flag.

Why is not accepting the tfvars file if Terraform uses the terraform.tfvars as default and I just want to change it?

Thanks

Are you sure you shared the full command used here?

Yes, the command as shown looks correct, but obviously Terraform is not able to parse the -var-file argument for some reason. If you run the command with TF_LOG=trace it will show the exact cli arguments as seen by terraform, and may highlight whatever the problem is.

1 Like

Still nothing, it shows the same error

terraform plan -var-file=test.tfvars TF_LOG=trace

│ Error: Too many command line arguments

│ To specify a working directory for the plan, use the global -chdir flag.

For more help on using this command, run:
terraform plan -help

FYI I am using this version:
Terraform v1.0.8
on windows_amd64

Hi, yes this is the full command:

terraform plan -var-file=test.tfvars

Where “test” is the name of the variable file I previously created

Thanks.

@vnicolas16,

Sorry, TF_LOG here is an environment variable, not a command line argument. Set TF_LOG=trace in the environment before running the command, and the trace logs will be output showing how terraform is parsing the arguments.

Hi,
This is the result of adding the TF_LOG=trace variable to the environment, any thougts

On one of the early lines of the logs we can see how the shell (Powershell in your case) presented the individual arguments to Terraform. In there I can see that for some reason PowerShell is treating .tfvars as a separate argument rather than as part of the argument containing the -var-file option.

Unfortunately PowerShell has various bugs like this when running traditional CLI commands, because it’s primarily designed for running its own “cmdlets” and so has to make some assumptions and guesses about your intent when you are running a command that isn’t integrated with PowerShell.

I would suggest running Terraform in the traditional windows Command Prompt (cmd.exe) instead, because it typically does not corrupt the command line or require unusual quoting and escaping when running external programs.

Thanks, I have tried the command in CMD, and unfortunately, I got a different error

Is there any other way I can solve this?

I{d appreciate your input

Hi @vnicolas16,

These new error messages do at least confirm that the Windows command interpreter is correctly passing the full and correct filenames test.tfvars and prod.tfvars to Terraform, and it’s Terraform itself which can’t find the files.

I’m not sure if I’m reading all of the paths in your screenshot directly, but it looks like you’re running Terraform from the root of your VSCode workspace, but that these two variable files are in a subdirectory called “Vars (Variable Files)”. If that’s true, I think these errors are correct because the files are not in the current working directory where you are running Terraform. If you move those two .tfvars files up to the workspace root, or if you switch into the subdirectory first before running terraform plan ..., do you get a better result?

Alright now is working, I left aside this for a couple of days, and now that I turned on my computer it worked fine, only in CMD, not using Powershell

Anyways, thanks a lot for your help and suggestions, very useful