CodexBloom - Programming Q&A Platform

Terraform 1.4.3: implementing Dynamic Block for Nested Security Group Rules in AWS

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-27
terraform aws security-groups hcl

I'm dealing with I'm writing unit tests and I'm working on a project and hit a roadblock. I'm working with an scenario while trying to create a security group in AWS using Terraform 1.4.3. I want to dynamically generate inbound rules based on a list of CIDR blocks, but I'm hitting a wall with the syntax. My goal is to create a security group that allows traffic from multiple IP ranges but I'm getting a `"behavior: Invalid value for "ingress" (each rule must be defined separately)"` behavior. Here's a snippet of what I've tried: ```hcl variable "allowed_ips" { type = list(string) default = ["192.168.1.0/24", "10.0.0.0/16"] } resource "aws_security_group" "my_sg" { name = "my-security-group" description = "Security group for my application" dynamic "ingress" { for_each = var.allowed_ips content { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = [ingress.value] // This seems incorrect } } } ``` I've tried changing how I reference the value in the dynamic block by using `ingress.value` instead of directly referring to the variable, but it still throws the same behavior. I've also attempted to replace the `cidr_blocks` with a single string, but that did not solve the question either. What is the correct approach for using dynamic blocks to define nested security group ingress rules? Any pointers would be greatly appreciated! This is part of a larger web app I'm building. I've been using Hcl for about a year now.