Issue running terraform plan with generate-out-config

with the new feature available from terraform 1.5.0 of generating config from new import section. i was trying to use it to import a key vault into terraform but when running a plan i am getting the following error : To Specify a working directory for the plan, use the global -chdir flag.
the command i am running is : terraform plan: terraform plan -generate-config-out=generated_resources.tf"

looks to be doing everything right but not sure what is causin this.

Hi @gaursumitkukku,

That message is from the Too many command line arguments error. Make sure you don’t have any other arguments after the plan flags. You can see the exact arguments as parsed by the shell in the trace logs with TF_LOG_CORE=trace.

Try putting the file name in quotes, like -generate-config-out=generated_resources.tf

Another way this can potentially go wrong is if you are running Terraform with PowerShell on Windows.

There are some long-standing quirks in PowerShell where it tokenizes the arguments for an external program in unexpected ways, which then get encoded by PowerShell and passed to Terraform as a different string than you typed on the command line. Terraform can’t tell this has happened and so it reports whatever problem it encountered in terms of what PowerShell provided, rather than in terms of what you typed.

I’ve previously heard of PowerShell struggling with the -var=filename.tfvars syntax and passing it to Terraform as if it were two separate arguments, which causes the same error message you saw here. Since this new option also follows the -option=filename shape, it’s likely that PowerShell will handle it in the same way and thus be subject to similar quirks.

If you are using PowerShell on Windows then the easiest workaround would be to use the Windows Command Interpreter (cmd.exe) instead, since it doesn’t attempt to parse the command line itself and so exactly the string you type (aside from some infrequently-used escape sequences) will be received by Terraform.

1 Like

Thanks @apparentlymart, i was indeed using the powershell to run the tf (creature of habit). when running it on the cmd it worked as expected. thank you very much,

Thanks @gregg.flynn , it is working now.