Api 网关 VPC link 由于安全组问题,无法通过 NLB 集成到 EC2 机器

Api gateway VPC link integration to a EC2 machine through the NLB not working because of the security group issue

我在私有子网中的 EC2 机器的端口 8080 上有一个服务 运行。我的计划是使用网络负载均衡器建立到专用端点的 VpcLink。现在网络负载均衡器无法关联自己的安全组,而是目标安全组将在这里起作用(在本例中是我的服务所在的 EC2 的安全组 运行)。我对 ec2 机器的安全组是什么样子有点困惑。我需要添加一个自定义 TCP 规则,允许流量到端口 8080,但我对我可以在源中提供的 IP 范围感到困惑。 API 网关没有 IP。另外,它说

Recommended Rules
Inbound Source      Port Range        Comment
Client IP addresses instance listener Allow traffic from clients on the instance listener port

VPC CIDR            health check      Allow traffic from the load balancer on the health check port

根据评论和 docs

Network Load Balancers do not have associated security groups. Therefore, the security groups for your targets must use IP addresses to allow traffic from the load balancer.

If you register targets by IP address and do not want to grant access to the entire VPC CIDR, you can grant access to the private IP addresses used by the load balancer nodes.

因此,对于 NLB,您可以使用 NLB 所在子网的 CIDR 范围。或者使用 NLB 专用地址对您进行更多限制。对于 IP 地址,您无法从 CloudFormation 获取它们。必须在 CloudFormation 中编写自定义资源才能从 NLB 获取 IP。作为折衷方案,您可以将 NLB 放在专用子网中,或与实例放在同一子网中,并使用其 CIDR 范围。