带有互联网网关的 VPC 中的 AWS Lambda 函数仍然无法访问互联网

AWS Lambda Function in VPC With Internet Gateway Still Can't access Internet

我有一个 lambda 函数,它只执行 http.gethttp://www.google.com。如果我没有 VPC 背后的功能,它工作正常。当我把它放在我的 VPC 中时,问题就出现了。

我知道您需要设置一个 Internet 网关。我这样做了。我的两个子网连接到路由 table,路由 0.0.0.0/0 到此 Internet 网关。这不应该是我所需要的吗?

无论 Internet 网关的关联如何,该函数仍然挂起。子网的安全组允许来自 0.0.0.0/0".

的所有流量

根据 Grant Internet Access to a VPC Lambda Function 这就是我需要做的一切。

编辑:

添加完整的 VPC 组件列表以使其清晰。

据我所知,我已经完成了该 AWS 文档 link 中的所有工作,以便为我的 Lambda 提供互联网访问权限。然而,当它试图向外部互联网发出请求时,它仍然永远挂起。

你快到了。您提供的 link 直接解决了您的问题:

If your function also requires internet access (for example, to reach a public AWS service endpoint), your function must use a NAT gateway or instance

你错过了这个:

Your VPC should contain a NAT gateway or instance in a public subnet.

这意味着如果没有 NAT,您的 Lambda 将无法访问互联网 - 即使 "they are" 在 public 子网中。这就是 lambda 在 VPC 中的基本工作方式。

与您提供的完全相同的 link 指导您如何与 VPC 和 Lambda 一起创建此 NAT 网关。

补充答案 - 为什么在这种情况下你需要 NAT 网关 - 是由于:

... you can use a network address translation (NAT) gateway to enable instances in a private subnet to connect to the internet or other AWS services, but prevent the internet from initiating a connection with those instances...

Extracted from aws docs


请记住:如果您需要您的 lambda 表达式访问仅互联网 - 而不是同一 VPC 中的任何其他资源 -我建议制作它们 non-VPC 然后它们将开箱即用地访问互联网 - 而且您无需支付 NAT 的费用。