As per the aws_alb you need a subnet_mappingblock for each subnet you are associating. And, in your situation, there is the need to have the number of block vary based upon the number of subnets you need to associated for the aws_alb in a given region.
In order to use the for_each you need to pass in a map or a set. Using a set by having the following:
for_each = toset(val.listvariable)
Will not provide you with an index of the item in the list (which you need to have in order to reference other attributes of other resources based upon the index of the value in the original list).
Therefore you will have to construct a map which provides you a way of referencing the index of that value as it was in the list.
This can be done as follows:
mymap = { for idx, val in local.mylist : idx => val }
Which given mylist = ["a", "b", "c", "d", "e"] will produce:
Note you could reverse the idx => val to val => idx to have the map key be the string
See the below HCL illustration. You should be able to copy/paste this into a .tf file in a separate directory and run terraform init / apply to see what it is doing and to experiment with changes to the code.
locals {
mylist = ["a", "b", "c", "d", "e"]
mymap = { for idx, val in local.mylist : idx => val }
}
resource "null_resource" "pretend_I_am_a_dynamic_block" {
for_each = local.mymap
triggers = {
mylistvalue = each.value
mylistindex = each.key
somthingelse = "some.resource[${each.key}].some_attribute"
}
}
output "mymap" {
value = local.mymap
}
output "null_resources" {
value = null_resource.pretend_I_am_a_dynamic_block
}
though likely not going to be what I want and more of a limitation of terraform.
i want to do this dynamically as I have 2 regions with a different number of AZs.
But since I can’t use count for this and have to use local vars to accomplish, it’s a not something i want to do. Though doesnt seem like I have a choice.
Sorry, just picking up on this after being a bit swamped.
My use of locals above for the mylist is just for illustration in the code. This local variable could easily be replaced by an input variable to make it dynamic and passed in at plan/apply time.
The mymap would be constructed dynamically based upon the values in the input variable and this would still be used in the dynamic block as you required.
variable "inputlist" {
type = list(string)
default = ["a", "b", "c", "d", "e"] # <--- Note this is again just for illustration
}
locals {
# original local variable in this position replaced by above and reference changed in next line.
mymap = { for idx, val in var.inputlist : idx => val }
}
## Remainder of code here