i have the following terraform:
variable "some_list" {
type = list(string)
default = ["a", "b", "c", "d", "e"]
}
// this map has values that correspond to values in var.some_list, but only some of the
// entries in var.some_list have corresponding entries in this map
variable "some_map" {
type = map(string)
default = {
"x": "a"
"y": "c"
"z": "e"
}
}
// create an instance of some_module for each entry in var.some_list
// this module as a policy ARN as one of its outputs
module "some_module" {
for_each = toset(var.some_list)
}
// create an instance of an aws_iam_role for each key in some_ma
resource "aws_iam_role" "some_role" {
for_each = var.some_map
name = each.key
assume_role_policy = data.aws_iam_policy_document.some_policy_doc[each.key].json
tags = module.tags.tags
}
// attach a policy to the role selected based on the keys in var.some_map using the policyArn output from some_module based on the corresponding values in var.some_map
resource "aws_iam_role_policy_attachment" "attachment" {
for_each = var.some_map
role = aws_iam_role.some_role[each.key].name
policy_arn = module.some_module[each.value].policyArn
}
when i execute this, i get these errors:
Error: Invalid index
on foo.tf line <x>, in resource "aws_iam_role_policy_attachment" "attachment":
<x>: policy_arn = module.some_module[each.value].policyArn
├────────────────
│ each.value is "a"
│ module.some_module is object with 2 attributes
Error: Invalid index
on foo.tf line <x>, in resource "aws_iam_role_policy_attachment" "attachment":
<x>: policy_arn = module.some_module[each.value].policyArn
├────────────────
│ each.value is "b"
│ module.some_module is object with 2 attributes
and so on for “c”, “d” and “e”.
module.some_module should have exactly one instance for each value in var.some_list
, so that lookup should work, shouldn’t it? what am i doing wrong?