当Terraform创建Security Group资源并从其他Security Group继承规则时,可以在配置中指定要使用的规则。例如,以下代码显示如何使用Terraform创建一个Security Group并仅继承指定的入站规则:
resource "aws_security_group" "example_sg" {
name_prefix = "example_sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.allowed_ssh.id]
}
}
resource "aws_security_group" "allowed_ssh" {
name_prefix = "allowed_ssh"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
}
}
在上面的代码中,创建了一个名为example_sg的AWS Security Group,并且只继承了allowed_ssh组的入站规则(允许SSH流量)。您可以根据需要使用此方法配置其他规则。