Workaround for error "An attribute name is required after a dot." when using for_each

I’m trying to use multiple aws_iam_policy_document output as input for module using for_each.

Example:

module "asg" {

for_each = local.asg 
......
name = each.key 
iam_policy  = data.aws_iam_policy_document.${each.value}.json
}

But getting error,

An attribute name is required after a dot.

Any workarounds or any other way to solve this?.

Terraform does not support dynamic references to arbitrary blocks.

That means you can’t write something like

To make this work, you’d have to restructure the multiple data blocks this is suggesting, to one data block with a for_each. Then, you can write something like:

data.aws_iam_policy_document.some_constant_name_here[each.value]