卓:为什么互联网网关执行 NAT?

AWS: Why does an Internet Gateway perform NAT?

An Internet gateway serves two purposes: to provide a target in your VPC route tables for Internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.

来源:https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Internet_Gateway.html

如果上述实例有 public 个 IPv4 地址,为什么互联网网关需要执行网络地址转换?最好有一个网络流的例子,在

的情况下会被破坏

尽管 EC2(或具体地说:与该 EC2 关联的 ENI)实例已与 public IP 地址关联。它对此一无所知!

这与原始 link 中共享的相同 URL 相同。 “您的实例只知道 VPC 和子网中定义的私有(内部)IP 地址 space”

这意味着进入 ENI 的所有网络流量 out/coming 都是通过私有 IP 地址。

如果您查看同一页上的图表,您会看到一个路由器。 这个路由器

  • 是否在 VPC 内跨其所有子网进行路由
  • 是否路由到 Internet 网关

EC2/ENI 将与此路由器交互。

路由器将流量定向到互联网网关 (IGW) 后: IGW 映射了该 VPC 内的 EC2 实例的 (public ip - private ip )。

EC2向互联网发送流量的步骤

  • 具有私有 10.0.1.1 IP 的 EC2 将请求发送到 google.com (curl google.com) 或尝试访问 public aws 构造,如 s3
  • 我们假设这是 public 子网。因此,与此子网关联的路由器会将此流量转发到默认路由 (0.0.0.0/0) 到 IGW
  • IGW查找其内部table(私有ip:public ip)。 NAT 转换是否使用 table.

我很想描述你的场景,但我不明白为什么 IGW 不执行 NAT。

弹性网卡实例侧只有私有地址。 Public 地址是通过关联弹性 IP 或分配 public 地址来完成的。这些 public 地址的映射来自互联网网关的 NAT 功能。

Your instance is only aware of the private (internal) IP address space defined within the VPC and subnet. The Internet gateway logically provides the one-to-one NAT on behalf of your instance, so that when traffic leaves your VPC subnet and goes to the Internet, the reply address field is set to the public IPv4 address or Elastic IP address of your instance, and not its private IP address. Conversely, traffic that's destined for the public IPv4 address or Elastic IP address of your instance has its destination address translated into the instance's private IPv4 address before the traffic is delivered to the VPC.

引自问题中链接的同一个 documentation

入站流量为 Public Address -> Internet Gateway -> Private Address

如果主机位于没有路由 table 且具有通过 IGW 定义的默认路由的子网中,则该实例将无法 return 流量。