使用to_port和from_port配置映射Terraform入口/出口的专用网络端口



我不确定这里的Terraform端口转发命名法。如果我在专用网络中的5000端口上有一个应用程序,我想在8000端口上向公众公开它,我应该设置哪些变量?

这会是从私人网络的角度来看吗?

from_port: 5000
to_port: 8000

还是公共网络的视角?

from_port: 8000
to_port: 5000 

如果我的内部应用程序需要用数据进行响应,我是否也需要设置出口值?

示例来自:地形简介

resource "aws_security_group" "elb" {
name = "terraform-example-elb"
# Allow all outbound
egress {
from_port   = 0
to_port     = 0
protocol    = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Inbound HTTP from anywhere
ingress {
from_port   = 80
to_port     = 80
protocol    = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

将安全组(SG(视为实例周围的一个封闭的气泡。它与实例内部发生的事情无关。它在实例之外运行。

您设置的SG规则,在这个气泡中打洞,指定允许哪些端口上的流量(TCP、UDP(进入实例和实例外部。

在您的情况下,由于您希望允许端口8000上的传入流量到达您的实例,因此您将生成一个";"洞";带有端口8000:

resource "aws_security_group" "elb" {
name = "terraform-example-elb"
# Allow all outbound
egress {
from_port   = 0
to_port     = 0
protocol    = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Inbound port 8000 from anywhere
ingress {
from_port   = 8000
to_port     = 8000
protocol    = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

上述SG将允许端口8000上的传入流量到达您的实例。不允许使用其他端口,因此from_port == to_port。一旦此流量被允许,您的实例正在做什么,这就超出了SG的范围和规则。因此,通常情况下,实例上必须有一台服务器在8000端口上侦听。如果您的应用程序在端口5000上工作,那么您需要反向代理,例如实例上的nginx。Nginx将接受端口8000上的连接,并将它们重定向到端口5000上的应用程序:

client--->SG:8000--->Instance:8000--->Nginx:8000--->Your app:5000

相反,如果你写

from_port: 5000
to_port: 8000

您将打开从5000到8000的所有端口以穿过气泡并到达您的实例。

最新更新