如何在启用 NAT 的子网中使用 AWS VPC 端点?

How to use AWS VPC endpoints in NAT enabled subnets?

我正在使用几个位于私有子网内的 AWS Lambda 函数,
这些私有子网为函数需要访问的服务配置了 VPC 端点,
当前设置不使用 NAT 网关,因此来自函数的所有流量都通过 VPC 端点。

我现在有一个需要使用 NAT 网关的用例,
但是启用 NAT 是否意味着 Functions 将不再使用 VPC 端点进行外部服务访问,而是使用 NAT?

我认为它的工作原理如下。对于:

网关端点(S3、DynamoDB)

当您创建它们时,它们的路由会自动添加到我们的路由表中。文档 says

If you have an existing route in your route table for all internet traffic (0.0.0.0/0) that points to an internet gateway, the endpoint route takes precedence for all traffic destined for the service, because the IP address range for the service is more specific than 0.0.0.0/0. All other internet traffic goes to your internet gateway, including traffic that's destined for the service in other Regions.

接口 VPC 端点

它们通过修改服务的 DNS 中的 IP 地址来工作。 IP 地址将是端点接口的专用地址。文档 says:

The hosted zone contains a record set for the default DNS name for the service (for example, ec2.us-east-1.amazonaws.com) that resolves to the private IP addresses of the endpoint network interfaces in your VPC. This enables you to make requests to the service using its default DNS hostname instead of the endpoint-specific DNS hostnames.

To use private DNS, you must set the following VPC attributes to true: enableDnsHostnames and enableDnsSupport.

结论

所以在这两种情况下,优先考虑的是接口,而不是互联网。我建议检查提供的链接。他们有更多信息和示例来仔细检查我的结论。

VPC 端点或 NAT 网关?

EC2、RDS、Lambda 和 ElastiCache 等 AWS 服务带有弹性网络接口 (ENI),它支持通过私有端点从 VPC 内部进行通信。但是,许多 AWS 服务提供 REST API,只能通过 Internet 获得。一些示例:S3、DynamoDB、CloudWatch、SQS 和 Kinesis。

可通过三个选项使这些服务可从私有子网访问:

  • A VPC Endpoint type: Gateway Endpoints 是免费的,但仅适用于 S3 和 DynamoDB。
  • A VPC Endpoint type: Interface Endpoint 的费用为每月 .20,AZ 加上每 GB [=11=].01,适用于大多数 AWS 服务。
  • NAT Gateway 可用于访问 AWS 服务或具有 public API 的任何其他服务。成本为每月 .40,AZ 加上每 GB [=13=].045

在设计网络架构时,请牢记以下经验法则。

  • 为 S3 和 DynamoDB 添加网关端点应该是您的默认选项。
  • 是否需要通过Internet访问非AWS资源,添加NAT网关。如果到 AWS 服务的流量证明额外的接口端点是合理的,请计算一下。
  • 您是否只从私有子网访问 AWS 服务?不超过四种不同的服务?使用接口端点。否则,计算接口端点和 NAT 网关的成本。

参考 Link: https://cloudonaut.io/advanved-aws-networking-pitfalls-that-you-should-avoid/