terraform plan -var=‘image_id_map={“us-east-1”:“ami-abc123”,“us-east-2”:“ami-def456”}’
Result in the error
Error: Value for undeclared variable
A variable named "'image_id_map" 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.
But it was declared in the variables.tf file.
variable “image_id_map” {
type = “map”
default = {}
}
I have tried the below format too
terraform plan -var=image_id_map={“us-east-1”:“ami-abc123”,“us-east-2”:“ami-def456”}
Result in this error
Error: Variables not allowed
on <value for var.image_id_map> line 1:
(source code not available)
Variables may not be used here.
Error: Variables not allowed
on <value for var.image_id_map> line 1:
(source code not available)
Variables may not be used here.
Terraform version: v0.12.8
provider.aws: version = “~> 2.35”
provider.template: version = “~> 2.1”
The exact syntax for variable values directly on the command line unfortunately varies based on which operating system and which shell you are using. The examples in Terraform’s documentation often assume a Unix-style shell, like bash, and may require some changes to work in other shells or on other operating systems such as Windows.
It looks like for you the shell interpreted the ' as part of the variable name, giving 'image_id_map. That is a behavior I’d expect on a Windows system; are you using Windows?
The Windows command line processing rules don’t include a way to disable quote interpretation for a sequence of characters like ' does in a Unix-style shell, so we need to use backslash escaping instead:
terraform plan -var image_id_map={\"us-east-1\":\"ami-abc123\",\"us-east-2\":\"ami-def456\"}
Because of the quite different interpretations of certain characters across different operating systems and shells, I’d recommend using -var arguments only for simple string values and using -var-file with .tfvars files instead for complex values like this. That way, only Terraform’s own parser is involved and the behavior will be consistent across all platforms. If you put this value in example.tfvars like this: