AWS Lambda HTTP 可从 Internet 访问,但不能从同一帐户的 EC2 访问

AWS Lambda HTTP accessible from the internet, but not accessible from an EC2 on the same account

我有一个 lambda 函数,它附加了一个 API 网关。它有一个可公开访问的 IP 地址和一个域名(由无服务器框架创建)。我可以通过我自己的浏览器(和其他设备)从广泛的互联网访问它。

问题是,它似乎无法从我帐户上的其他服务访问,例如 EC2 实例。从提到的实例中,curl google.com 工作并且 returns 响应,但 curl mylink.com 超时。

我认为这与 VPC 设置有关,但我无法确定。这可能是什么问题?

更新:使用 curl --verbose,我发现从其他任何地方访问时,API 网关指向的 IP 与从我的 AWS 帐户内访问时不同。 VPC好像无所谓,我新建了一个VPC,里面有一个EC2实例,还是一样。

EC2 实例可能位于没有 public 互联网访问权限的 VPC 中(或者可能但通过负载均衡器、堡垒主机或任何其他选项)。

您需要找出 EC2 实例所在的 VPC,并确保您的 lambda 函数可以访问它。您可以按照 the documentation

向 VPC 添加功能

根据评论。

问题是由 VPC interface endpoint for API Gateway in the default VPC. The VPC interface endpoint for API gateway is used for private API, not public APIs as used be the OP. Subsequently, calls to public API endpoints fail, as explained in AWS blog 的存在引起的:

When they’re configured as private, the public networks are not made available to route your API. Instead, your API can only be accessed using the interface endpoints that you have configured.

有两种方法可以解决这个问题:

在 OP 的案例中,第二个选项用于解决问题。