I've been writing reusable modules for an AWS infrastructure. In creating a security group, my approach is to create a generic module for a security group and provide a list of ports in the control code. However, when using count
it creates a security group each for every port. Is there a way around this to iterate a specific part like in this scenario?
SG Module
resource "aws_security_group" "this" {
name = var.sg_name
description = var.description
vpc_id = var.vpc_id
count = min(length(var.ingress_ports))
ingress {
from_port = var.ingress_ports[count.index]
to_port = var.ingress_ports[count.index]
protocol = "tcp"
cidr_blocks = ["10.0.0.0/8"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Control Code
module "qliksense_sg" {
source = "modules/aws-sg"
sg_name = "My-SG"
description = "A security group"
vpc_id = module.vpc.vpc_id
ingress_ports = ["80", "443"]
}