AWS Security Groups Dynamically with Mulitple IPs and Ports

Hello,

Would need one help on the issue. I am about to create AWS Security group from the list of IPs and Ports. I know how to use Dynamic blocks and below is my code. Here my query is without adding “Dynamic Ingress” blocks multiple times, how to achieve this problem. Please help.

provider “aws” {
profile=“source”
region=“us-east-1”
}

variable “sg-ports” {
type=list(number)
default = [80,443,22,21,20]
}

variable “inbound_ip” {
type=list(string)
default=[“1.1.1.1/32”,“2.2.2.2/32”,“3.3.3.3/32”,“4.4.4.4/32”]
}

resource “aws_security_group” “sg1” {
dynamic “ingress” {
for_each=var.sg-ports
iterator=port
content{
from_port=port.value
to_port=port.value
protocol=“tcp”
cidr_blocks=[var.inbound_ip[0]]
}
}

dynamic “ingress” {
for_each=var.sg-ports
iterator=port
content{
from_port=port.value
to_port=port.value
protocol=“tcp”
cidr_blocks=[var.inbound_ip[1]]
}
}

dynamic “ingress” {
for_each=var.sg-ports
iterator=port
content{
from_port=port.value
to_port=port.value
protocol=“tcp”
cidr_blocks=[var.inbound_ip[2]]
}
}
}

Regards,
Jana