Getting │ Error: Invalid index on line 21

Hello Everyone,

I am getting below error when executing terraform plan for Production workspace:

│ Error: Invalid index
│ on line 21, in locals:

21:   aws_clause = "(region LIKE '${var.prod_region[terraform.workspace]}' OR aws.region LIKE '${var.prod_region[terraform.workspace]}' OR tags.ProdRegion LIKE '${var.prod_region[terraform.workspace]}' OR ProdRegion LIKE '${var.prod_region[terraform.workspace]}' OR label.ProdRegion LIKE '${var.prod_region[terraform.workspace]}') AND (aws.accountId  = '${var.aws_acct_id[terraform.workspace]}' OR tags.accountId = '${var.aws_acct_id[terraform.workspace]}') SINCE 7 DAYS AGO TIMESERIES"

              | terraform.workspace is "Prod"

              |  var.prod_region is "{\"Prod\":\"eu-central-1\"}"

This value does not have any indices.

I am not getting what’s wrong here, though when I tried manual hard coded values instead of above variables in clause, it works well!

Please advise.


Hi @sunny.bhadesiya,

Based on the error message’s description of var.prod_region it seems like that variable contains a string with a JSON encoding of an object inside of it, rather than an actual object value.

If you wish to use the data from there you will need to do one of the following:

  • Change the variable’s type to be a map of strings, and pass in a real map of strings value rather than a JSON string.
  • Decode the JSON using jsondecode first, and then access the attribute name given in terraform.workspace from that result.