连接 AWS EC2 实例以避免 ip 更改时连接失败的最佳方法

Best way to connect AWS EC2 instances for avoid failed connection on ip change

我有四个 EC2 实例,其中三个 运行 api 服务和另一个 运行 用户界面 (UI)。 UI 实例通过 api 调用另一个实例获取数据。现在一切正常,因为我使用为每个 EC2 服务提供的 public IP 进行 api 调用。但是,我关心的是如果服务的 public ip 发生变化(出于任何原因)会发生什么?然后我的应用程序停止运行,因为 UI 无法从服务中获取数据。经过一些研究后,我发现 appers 是一个解决方案:使用 vpc 通过私有 ip 连接 EC2 实例(因为是静态的)并将 UI 实例关联到弹性 IP(这里没问题)。母猪,我有一些问题:

1) 我做了一个测试,将所有实例放在同一个 vpc(和子网)中,但是当我从一个实例到另一个实例时,ping 失败了。我的方法对吗?或者我遗漏了什么?

2) 我阅读了其他几个选项,但我不确定什么是最好的:也许我必须使用 Api 网关?。还是 NAT 网关?

3) 以私有方式通信 EC2 实例的标准做法是什么?

1.) 您需要更新具有 ICMP 流量权限的安全组。 转到您的 VPC -> Select 安全组 -> Select 相关安全组 -> 为所有具有实例子网 CIDR 的流量添加 Inbound/Outbound 规则。

2.) 内部网络是更好的方式,只要您的所有流量都在内部。

谢谢

1) 我做了一个测试,将所有实例放在同一个 vpc(和子网)中,但是当我从一个实例到另一个实例时,ping 失败了。我的方法对吗?或者我遗漏了什么?

For security reasons, AWS block the ICMP traffic using security group. Please enable Ping traffic (ICMP) in security group from the Ip's you are trying to connect, it's better to allow the entire CIDR block for the VPC for all traffic, will make your life a lot easy. Please make sure you do this in a test Environment only.

2) 我阅读了其他几个选项,但我不确定什么是最好的:也许我必须使用 Api 网关?。还是 NAT 网关?

Also, as you mentioned that your concern is that the public IP of the Instance will change, (definitely if your Instance stop/starts for any reason), but why don't you use Elastic IP for all of your Instances, that could be on of the solution, but using this approach all of your instances will be exposed to internet, so going with private IP is the best option.

3) 以私有方式通信 EC2 实例的标准做法是什么?

It depends on the use case, if your Instances are in the same vpc no extra configuration is required, you only need to make sure the security groups, Network Access Control List and firewall configuration are correct. In case if your instances are in different VPC, then you can use VPC Peering/Transit gateway.