CodexBloom - Programming Q&A Platform

Terraform: implementing Dynamic Block for AWS Security Group Rules Not Applying Correctly

šŸ‘€ Views: 9166 šŸ’¬ Answers: 1 šŸ“… Created: 2025-06-14
terraform aws security-group hcl

I've spent hours debugging this and I'm performance testing and I'm relatively new to this, so bear with me. I'm experimenting with I'm experiencing an scenario with a dynamic block in Terraform when trying to configure multiple ingress rules for an AWS security group. I'm using Terraform version 1.3.5 and my code looks like this: ```hcl resource "aws_security_group" "example" { name = "example-sg" description = "Security group for example" 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 description = ingress.value.description } } } ``` Here, `var.ingress_rules` is a variable defined as follows: ```hcl variable "ingress_rules" { type = list(object({ from_port = number, to_port = number, protocol = string, cidr_blocks = list(string), description = string })) } ``` When I run `terraform apply`, I get the following behavior: `behavior: Unsupported attribute` for `ingress.value`. It seems like I'm not accessing the elements correctly within the dynamic block. I've tried using `each.value` instead of `ingress.value`, and I have also defined the variable in various ways, but I still face the same scenario. This is my input variable in `terraform.tfvars`: ```hcl ingress_rules = [ { from_port = 80, to_port = 80, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"], description = "Allow HTTP" }, { from_port = 443, to_port = 443, protocol = "tcp", cidr_blocks = ["0.0.0.0/0"], description = "Allow HTTPS" } ] ``` I’m unsure how to correctly reference the `ingress` block's attributes. Any help would be appreciated! I'm developing on Debian with Hcl. What are your experiences with this? Any ideas how to fix this? Any suggestions would be helpful.