【发布时间】:2019-03-18 22:45:46
【问题描述】:
我在 terraform 中定义了这个 AWS 安全组:
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"]
}
}
使用此配置,任何端口都可以用作传出/出站。但是如果我想排除一些端口,推荐的方式是什么?
假设我想排除端口 25 和 465,所以我可以执行类似的操作(而不是使用允许任何端口的出口规则):
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"]
}
但这需要定义具体的范围,这需要定义一些额外的出口规则。也许有更好的方法吗?例如,我可以在哪里定义规则以允许所有端口然后排除一些?
【问题讨论】:
-
很确定,你不能 ;-( 如果你有很多排除项,你可以编写一个包装脚本来吐出所需的 TF 代码,但是根据端口的数量,你可能最终达到每个 SG 的最大规则数。
标签: terraform terraform-provider-aws