Terraform: Trouble with Dynamic Block for Creating Multiple Security Group Rules Based on Input List
I've searched everywhere and can't find a clear answer... I'm writing unit tests and I'm performance testing and This might be a silly question, but I'm trying to dynamically create multiple security group rules in AWS using Terraform, but I keep running into issues with the way I'm attempting to structure my code... I'm using Terraform version 1.3.6 and I have a variable that holds a list of maps representing the security group rules I want to create. The variable is defined as follows: ```hcl variable "security_group_rules" { type = list(object({ cidr_blocks = list(string) from_port = number to_port = number protocol = string })) } ``` Then, I'm trying to create the rules within the security group resource like this: ```hcl resource "aws_security_group" "example" { name = "example_sg" description = "Example Security Group" dynamic "ingress" { for_each = var.security_group_rules content { from_port = ingress.value.from_port to_port = ingress.value.to_port protocol = ingress.value.protocol cidr_blocks = ingress.value.cidr_blocks } } } ``` However, when I run `terraform apply`, I'm getting the following behavior: ``` behavior: Invalid function argument on main.tf line 10, in dynamic "ingress": 10: for_each = var.security_group_rules The argument must be a list or a set, but it was a map. ``` It seems like Terraform is interpreting the `for_each` as a map instead of a list, which is surprising because I confirmed that my input variable is indeed a list of objects. I have also tried using `to_list()` around `var.security_group_rules`, but that didn't seem to help either. Can anyone provide insights on what I might be doing wrong here? Any guidance on how to properly structure this dynamic block would be greatly appreciated! This is part of a larger CLI tool I'm building. I'd really appreciate any guidance on this. I'm working in a macOS environment. Hoping someone can shed some light on this. The stack includes Hcl and several other technologies. This is happening in both development and production on Windows 11. Am I missing something obvious?