CodexBloom - Programming Q&A Platform

Terraform Trying to Use a Dynamic Block for Security Group Rules but scenarios to Apply Correctly

šŸ‘€ Views: 95 šŸ’¬ Answers: 1 šŸ“… Created: 2025-06-26
terraform aws security-group HCL

I need help solving I've been working on this all day and I'm working with Terraform 1.3.5 to manage AWS security groups, and I'm trying to use a dynamic block to create a list of ingress rules based on a variable. However, when I apply the configuration, I'm working with an behavior that says `Invalid parameter: IpProtocol` and it seems to be ignoring the rules I've defined. I set up the configuration as follows: ```hcl variable "ingress_rules" { type = list(object({ from_port = number to_port = number protocol = string cidr_blocks = list(string) })) } resource "aws_security_group" "example" { name = "example_sg" description = "Example security group" dynamic "ingress" { for_each = var.ingress_rules content { from_port = ingress.value.from_port to_port = ingress.value.to_port protocol = ingress.value.protocol cidr_blocks = ingress.value.cidr_blocks } } } ``` I have a variable file that sets `ingress_rules` like this: ```hcl ingress_rules = [ { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }, { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["192.168.1.0/24"] } ] ``` But when I run `terraform apply`, I get the following behavior message: ``` behavior: Invalid parameter: IpProtocol on main.tf line 10, in resource "aws_security_group" "example": 10: protocol = ingress.value.protocol ``` I've double-checked that the values in `ingress_rules` are correctly formatted, and I'm able to create security groups with static rules just fine. It seems like the dynamic block isn't processing the variable correctly. Is there something I’m missing in the way the dynamic block is structured? Any help would be appreciated! My development environment is Linux. Any examples would be super helpful. My team is using Hcl for this REST API. Any ideas what could be causing this? For context: I'm using Hcl on Debian. I'm open to any suggestions.