简体   繁体   English

如果 EC2 实例未对公众开放,AWS 网络负载均衡器无法连接到 EC2 实例

[英]AWS Network Load Balancer failed to connect with EC2 instance if EC2 instance is not open to public

I have been struggling with this problem for 2 days but couldn't get it working.我一直在为这个问题苦苦挣扎 2 天,但无法让它工作。

I have this flow:我有这个流程:

external world --> AWS API Gateway ---> VPC Link ---> Network Load Balancer ---> my single EC2 instance外部世界 --> AWS API 网关 ---> VPC 链接 ---> 网络负载均衡器 ---> 我的单个 EC2 实例

Before introducing the API Gateway, I want to first make sure the Network Load Balancer --> my single EC2 instance part works.在介绍 API 网关之前,我想首先确保Network Load Balancer --> my single EC2 instance部分有效。

I have set up the EC2 instance correctly.我已经正确设置了 EC2 实例。 There is a Typescript / ExpressJS api service running on port 3001有一个 Typescript / ExpressJS api 服务在端口3001上运行

I have also set up a Network Load Balancer and a Target Group, the NLB is listening and forwarding requests to port 3001 of the target group (which contains the EC2 instance).我还设置了一个网络负载均衡器和一个目标组,NLB 正在侦听并将请求转发到目标组的端口3001 (其中包含 EC2 实例)。

Here is the NLB:这是NLB: 在此处输入图像描述

Note that the NLB has a VPC.请注意,NLB 有一个 VPC。 This raise the question below and I find it so confusing.这提出了下面的问题,我觉得很困惑。

listener:听众: 在此处输入图像描述

You can see it is forwarding requests to docloud-backend-service , which is described as follows:可以看到它正在将请求转发到docloud-backend-service ,描述如下: 在此处输入图像描述

You can see that the health check has passed.可以看到健康检查通过了。

I have configured the security group of my EC2 instance with this rule:我已经使用以下规则配置了我的 EC2 实例的安全组:

1. Allow All protocol traffic on All ports from my VPC 
(specified using CIDR notation `171.23.0.0/16`);

Now, when I do curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user , the command fails by timeout.现在,当我执行curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user时,命令因超时而失败。

Then, after I add this rule:然后,在我添加此规则后:

2. Allow All protocol traffic on All ports from ANY source (`0.0.0.0/0`);

Now, when I do curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user ,现在,当我做curl docloud-backend-xxxxx.elb.ap-northeast-1.amazonaws.com:3001/api/user时,

the api service gets the request and I can see logs generated in the EC2 instance. api 服务收到请求,我可以看到 EC2 实例中生成的日志。

Question:问题:

The second rule opens up the EC2 instance to public, which is dangerous.第二条规则向公众开放 EC2 实例,这是危险的。

I want to limit access to my EC2 instance port 3001 such that only the AWS API Gateway, or the NLB can access it.我想限制对我的 EC2 实例端口 3001 的访问,以便只有 AWS API 网关或 NLB 可以访问它。

The NLB has no security group to be configured. NLB 没有要配置的安全组。 It has a VPC though.虽然它有一个 VPC。 If I limit the EC2 instance such that only its own VPC can access it, it should be fine, right?如果我将 EC2 实例限制为只有它自己的 VPC 可以访问它,那应该没问题,对吧?

The first rule does exactly that.第一条规则正是这样做的。 Why does it fail?为什么会失败?

The NLB has a VPC. NLB 有一个 VPC。 Requests go from API Gateway to NLB, then from NLB to EC2 instance.请求 go 从 API 网关到 NLB,然后从 NLB 到 EC2 实例。 So from the EC2 instance's perspective, the requests come from an entity in the VPC.因此,从 EC2 实例的角度来看,请求来自 VPC 中的实体。 So the first rule should work, right?所以第一条规则应该有效,对吧?

Otherwise why would AWS assign a VPC to the NLB anyways?否则,AWS 为什么要为 NLB 分配一个 VPC?

Why would I see the VPC on the NLB's description console anyways?为什么我会在 NLB 的描述控制台上看到 VPC?

I want to limit access to my EC2 instance port 3001 such that only the AWS API Gateway, or the NLB can access it.我想限制对我的 EC2 实例端口 3001 的访问,以便只有 AWS API 网关或 NLB 可以访问它。

For instance based target groups and for IP based target groups as well we can enable/disable if want to preserve the requester's IP address:例如基于实例的目标组和基于 IP 的目标组,如果想要保留请求者的 IP 地址,我们可以启用/禁用:

在此处输入图像描述

This setting can be found if go to our target group -> Actions -> Edit Target attributes.如果 go 到我们的目标组 -> 操作 -> 编辑目标属性,可以找到此设置。

在此处输入图像描述

What does this mean from the perspective of the Security Group of our application?从我们应用程序的安全组的角度来看,这意味着什么?

If we enable it (which is the default for instance type target groups), the application will see traffic as it is coming directly from the end-client.如果我们启用它(这是实例类型目标组的默认设置),应用程序将看到直接来自终端客户端的流量。 This means, you we have to enable inbound traffic for 0.0.0.0:3001 .这意味着,我们必须为0.0.0.0:3001启用入站流量。

If we disable it, the application will see the source traffic as it was coming from the private IP address of the Network Load Balancer.如果我们禁用它,应用程序将看到来自网络负载均衡器的私有 IP 地址的源流量。 In this case, we can limit the inbound traffic to the private IP address of the NLB or to the CIDR range of the subnet in which the NLB is placed.在这种情况下,我们可以将入站流量限制为 NLB 的私有 IP 地址或放置 NLB 的子网的 CIDR 范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM