Home > Software design >  Terraform error A value of type string cannot be used as the collection in a 'for' express
Terraform error A value of type string cannot be used as the collection in a 'for' express

Time:01-15

I have created a module to add an AWS.APIGateway route and authoriser. The module is called depending on how many variables are in the tfsvars file. This variable is passed through to the module to create the Route name as extprovider.

I need to take this variable and append a suffix to it so that i can then add multiple clientids (audiences) to the authoriser.

Error: Iteration over non-iterable value

  on ../modules/apiresource/locals.tf line 3, in locals:
   3: extprovider_new  = flatten([ for e in var.extprovider : tolist(["${e}odd","${e}even"])])
    ├────────────────
    │ var.extprovider is "foo"

A value of type string cannot be used as the collection in a 'for' expression.

Error: Iteration over non-iterable value

  on ../modules/apiresource/locals.tf line 3, in locals:
   3: extprovider_new  = flatten([ for e in var.extprovider : tolist(["${e}odd","${e}even"])])
    ├────────────────
    │ var.extprovider is "bar"

A value of type string cannot be used as the collection in a 'for' expression.

My code is;

locals.tf

locals {
extprovider_new  = flatten([ for e in var.extprovider : tolist(["${e}odd","${e}even"])])
}
variables.tf
variable extprovider {}

CodePudding user response:

You are passing a string variable to module but in module you want to use it as list.

You can pass variable like

module "module_name" {
  ...
  extprovider = ["foo"]
  ...
}

and it should work or in locals.tf

locals {
   extprovider_new  = ["${var.extprovider}odd", "${var.extprovider}even"]
}

you can use string variable and convert it to list which you need.

  • Related