How to use for_each inside a terraform json configuration file


I am having trouble escaping or passing along a set to the for_each function when using a terraform json config file.

I can do this in a terraform file and everything works as expected:

for_each = {for e in local.email_ids: e.emails => e}

Here is what i tried for json syntax:

“for_each”: “${for e in local.email_ids: e.emails => e}”

That is not working. here is the error message:

23: “for_each”: “${for e in local.email_ids: e.emails => e}”,

Expected a closing brace to end the interpolation expression, but found extra

Anyone know the magic sequence?


never-mind. I got this to work using this instead.

“for_each”: “${toset(local.email_ids.emails)}”,


Hi @salmr0 ,

if you still have some interest you could try

            "${{for e in local.email_ids : e.emails => e}}"
1 Like