I have this AWS security group defined in terraform:
resource "aws_security_group" "sg" {
name = "${var.name}"
description = "${var.description}"
vpc_id = "${data.terraform_remote_state.vpc.vpc_id}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.ext_blocks}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
With this configartion, any port can be used as outgoing/outbound. But if I want to exclude some ports, what would be the recommended way?
Let say I want to exclude ports 25 and 465, so I could do something like (instead of using that one egress rule that allows any port):
egress {
from_port = 0
to_port = 24
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 26
to_port = 464
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 466
to_port = 65535
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
But this needs to define specific ranges, which require to define some extra egress rules. Is there a better way maybe? For example where I can define rule to allow all ports and then exclude some?