Expected the start of an expression, but found an invalid expression token

Hi Team,

when i am passing list as a variable i am getting below error.


cidr=['"'33.0.0.0/8'"','"'30.0.0.0/8'"','"'10.197.0.0/22'"']
echo "${cidr}"
terraform.exe apply   -var "VPC_id=vpc-05a5f975e4" -var "subnet_id=subnet-0455cef4f" -var "ami_id=ami-0d9ba49d82" -var 'cidr_blocks=${list(cidr)}'

Error: Invalid character

  on <value for var.cidr_blocks> line 1:
  (source code not available)

This character is not used within the language.


Error: Invalid expression

  on <value for var.cidr_blocks> line 1:
  (source code not available)

Expected the start of an expression, but found an invalid expression token.

Hi @sharathmankala,

It looks like you’ve tried to use template syntax and a function inside the definition of cidr_blocks. That isn’t supported, but it also isn’t necessary for your situation here because the cidr block list is already defined in the expected list constructor syntax, so you can pass it directly.

I see you running terraform.exe which makes me suspect you are using Windows, but you are also using Unix-style shell quoting so I suppose you are using a Unix shell ported to Windows? In which case, I think the following syntax should work, using double quotes because otherwise we can’t use shell interpolation of cidr:

terraform.exe apply -var "VPC_id=vpc-05a5f975e4" -var "subnet_id=subnet-0455cef4f" -var "ami_id=ami-0d9ba49d82" -var "cidr_blocks=$cidr"

Note that the $cidr here is interpreted by the shell, not by Terraform. Terraform will just see the value of that variable pasted in there exactly as you wrote it, without knowing it came from a shell variable.

If you are using the Windows command interpreter or PowerShell then you might need to use slightly different syntax, but the general idea is to use the shell interpolation to insert the value before Terraform even runs, rather than expecting Terraform to evaluate the argument as a Terraform template.