CodexBloom - Programming Q&A Platform

Terraform: scenarios 'Invalid Function Argument' When Using Terraform 1.4 with Dynamic Blocks for EC2 Security Groups

๐Ÿ‘€ Views: 52 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-11
terraform aws security-group HCL

I've searched everywhere and can't find a clear answer. I'm working through a tutorial and Could someone explain I'm stuck on something that should probably be simple..... This might be a silly question, but I'm working with a frustrating scenario while trying to set up a dynamic block for security group rules in Terraform 1.4. The setup requires me to create a security group with a variable number of ingress rules based on user input. However, when I apply the configuration, I get the behavior: `behavior: Invalid function argument`. Hereโ€™s the relevant part of my Terraform code: ```hcl variable "ingress_rules" { type = list(object({ from_port = number to_port = number protocol = string cidr_blocks = list(string) })) } resource "aws_security_group" "my_sg" { name = "my-sg" 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 } } } ``` The behavior is referring to the line where I attempt to access `ingress.value`. Iโ€™ve double-checked the structure of the `ingress_rules` variable and confirmed that it matches the expected format. As a quick test, I hardcoded a single rule directly into the `aws_security_group` resource, and it worked fine, which leads me to believe that the scenario is within the dynamic block. I've also tried running `terraform validate`, which didn't flag any issues, and I've ensured that the variable is being passed correctly. My input variable looks like this: ```hcl variable "ingress_rules" { default = [ { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }, { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["192.168.1.0/24"] } ] } ``` I suspect there might be a question with how I'm trying to reference the dynamic blockโ€™s value, but I need to figure out the correct syntax. Any help would be greatly appreciated! I'm working on a web app that needs to handle this. I'd really appreciate any guidance on this. Thanks in advance! This is part of a larger application I'm building. I'm working in a Linux environment. Any advice would be much appreciated. I'm working with Hcl in a Docker container on Windows 10. I'd love to hear your thoughts on this.