# Nested Dynamic Block for security group configuration

**URL:** <https://discuss.hashicorp.com/t/nested-dynamic-block-for-security-group-configuration/55040>\
**Category:** Terraform\
**Created:** [June 15, 2023, 12:59am UTC](https://discuss.hashicorp.com/t/nested-dynamic-block-for-security-group-configuration/55040 "2023-06-15T00:59:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nagendrakumar.nainar](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@nagendrakumar.nainar](https://discuss.hashicorp.com/u/nagendrakumar.nainar)\
**Post date:** [June 15, 2023, 12:59am UTC](https://discuss.hashicorp.com/t/nested-dynamic-block-for-security-group-configuration/55040/1 "2023-06-15T00:59:29Z")

</div>

Hi,

I have the below requirement:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [June 15, 2023, 2:29pm UTC](https://discuss.hashicorp.com/t/nested-dynamic-block-for-security-group-configuration/55040/2 "2023-06-15T14:29:40Z")

</div>

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!
