Nested Dynamic Block for security group configuration

Hi,

I have the below requirement:

source_ports = [8001, 8002, 8003]
destination_ports = [9001, 9002, 9003]

I have to configure AWS security group with policies allowing from each source port to each destination port (Ex: 8001->9001, 8001->9002, 8001->9003, 8002->9001…so on)

I am trying to understand if there is any nested dynamic block approach to configure the same?

I tried something like below and couldn’t make it work:

variable "ingress_ports" {
    type = list(number)
    default = [9001, 9002, 9003]
}

variable "egress_ports" {
    type = list(number)
    default = [8001, 8002, 8003]
}

resource "aws_security_group" "aws_sg" {
    name = "Terraform_SG_Learning"
    for_each = var.ingress_ports
    dynamic "ingress" {
        for_each = var.egress_ports
        content {
            from_port = ingress.value
            to_port = [for port in var.egress_ports : ${port}]
        }
    }
}

Any help/clarification is much appreciated.

Thanks,
Nagendra

Hi @nagendrakumar.nainar,

Please share what happened when you tried your solution. Seeing a specific error message or a description of how the behavior differed from what you intended is helpful to those trying to answer to make sure they are focused on the right problem. Thanks!