When i am running terraform plan -var 'users=["ankit","gaurav","kapil"]'
It throwing below error
╷
│ Error: Variables not allowed
│
│ on <value for var.users> line 1:
│ (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: Variables not allowed
│
│ on <value for var.users> line 1:
│ (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: Variables not allowed
│
│ on <value for var.users> line 1:
│ (source code not available)
│
│ Variables may not be used here.
╵
╷
│ Error: No value for required variable
│
│ on list.tf line 1:
│ 1: variable "users" {
│
│ The root module input variable "users" is not set, and has no default value. Use a -var or -var-file command line
│ argument to provide a value for this variable.
The simple config files you have here are fine, so my guess is that you are using a Windows shell where the single quotes around the argument are not sufficient to escape the argument string. If you run the command with TF_LOG=trace Terraform will show exactly what it received as command line arguments from the OS so you can see how they were interpreted before being handed off to Terraform.
PowerShell’s syntax for quoting and escaping is a little different to the syntax used on Unix shells. You can find the full details in about Quoting Rules.
Based on that documentation – in particular on including quote characters in a string – I would expect the following to work (but I don’t have a Windows machine handy, so I’ve not tested):
terraform plan '-var=users=["Aniket","Rohan","Nana"]'
Unfortunately what makes things more complicated here is that Terraform is not a PowerShell cmdlet and so there is an extra step: after parsing the command line, PowerShell must then use the result to build a command line string to pass to Terraform, which sometimes causes additional problems.
One way to avoid those problems is to tell PowerShell not to parse the arguments at all, by using the --% operator which causes everything after it to be passed literally to Terraform, and then Terraform’s own parser for Windows-style command line options – which follows the standard quoting conventions used by the C++ language runtime – will interpret it:
&terraform --% plan -var=users=[\"Aniket\",\"Rohan\",\"Nana\"]
However- because --% disables the PowerShell parser for these arguments you won’t be able to use any PowerShell variables or other expression syntax in the arguments when using this technique.
PowerShell’s treatment of the arguments for external programs is unfortunately quite idiosyncratic. It often works better to run Terraform using the traditional Windows command interpreter (cmd.exe) instead, because it doesn’t try to parse and then regenerate the arguments before passing it to Terraform.
Thanks for your quick response . I have tried to run terraform program in cmd also. Below is the response i got
C:\terraform-1.8>terraform plan -var 'users=["ankit","gaurav","kapil"]'
╷
│ Error: Value for undeclared variable
│
│ A variable named "'users" 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.
╵
C:\terraform-1.8> terraform plan -var 'users=[\"Aniket\",\"Rohan\",\"Nana\"]'
╷
│ Error: Value for undeclared variable
│
│ A variable named "'users" 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.
Although i have declared variable users also . The code is running in powershell but for cmd it is not.
variable "users" {
type = list
default = [ "aniket" ]
}
When you use cmd.exe to run Terraform you must use the typical Windows conventions for command line quoting, which means that single quotes ' are not supported and instead you will need to use backslash escaping on each of the quotes. This is admittedly annoying, but it’s the standard way for CLI programs on Windows to interpret their arguments, so Terraform follows it for consistency with other software.