VPC 中的 Lambda 函数无法访问 public 互联网
Lambda function within VPC doesn't have access to public Internet
我正在尝试从 Lambda 函数中向第三方服务发出出站 API 请求,但该函数总是超时且没有任何错误。
这以前发生在尝试在不同的功能(仍在相同的 VPC/子网)中执行 s3.putObject
操作时,我设法通过添加具有服务名称的端点来解决这个问题 com.amazonaws.us-east-1.s3
并将其连接到与此 Lambda 函数所在的 VPC 关联的路由 table。
在网络框 -> 安全组部分的 Lambda 仪表板中,我看到了这个警告:
When you enable VPC, your Lambda function will lose default internet
access. If you require external internet access for your function,
ensure that your security group allows outbound connections and that
your VPC has a NAT gateway.
我相信这个安全组允许出站连接,基于下面的出站规则 table:
对于第二个要求,我可以确认这个 VPC 有一个 NAT 网关,因为在 VPC 仪表板上,在 NAT 网关选项卡中,出现在那里的那个有一个与之关联的 VCP,并且那个 VPC 是同一个托管 Lambda 函数。
我按照指南创建了一个 Flow Log 来监控进出 VPC 的流量,希望看到那些出站请求确实被拒绝了。但是,这样做并检查 CloudWatch 日志后,所有记录都以 ACCEPT OK
或 NODATA
.
结尾
How can I grant internet access to my VPC Lambda function? 是我最初尝试遵循的指南,但我卡在了 To create a public or private subnet
:
下的第 4 步
From the Change to: drop-down menu, choose an appropriate route table:
For a private subnet, the default route should point to a NAT gateway
or NAT instance:
Destination: 0.0.0.0/0
Target: nat-… (or eni-…)
For a public subnet,
the default route should point to an internet gateway:
Destination: 0.0.0.0/0
Target: igw-…
对于此 VPC 中的所有四个子网,单击 Change to:
右侧的下拉列表仅显示一个选项,即已选择的选项 rtb-xxxxxxxx
。单击通往该路线 table 的 link 并单击“摘要”旁边的“路线”选项卡后,我看到了:
阻止 Lambda 函数访问 Internet 可能是我做错了什么?
要让 Lambda 通过 VPC 访问互联网,它应该在 Private Subnet
中并附加 NAT Gateway
。
根据您的屏幕截图,附加到子网的路由 table 已附加 igw-xxxxxxx
使您当前的子网成为 public subnet
.
因此,要使事情正常进行,您可以执行以下操作:
- 在您当前
subnet
的路线 table 中附加 NAT Gateway
而不是 igw-xxxxxx
或
- 找到连接到您的 Lambda 的 ENI 并附加
Elastic IP
如果您想要访问互联网。
或
根据@John Rotenstein,如果您的 Lambda 不需要 VPC 资源,您可以将 Lambda 移出 VPC
在 VPC
模式下设置 AWS Lambda 互联网访问很棘手,因为 AWS 方面没有提供适当的 AWS 文档,我确实多次阅读这篇文章 https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/ 但没有观看 YouTube 视频。经过这么多的研发,我可以总结出以下步骤是 在 VPC 模式下设置 AWS Lambda
所需要的
- Route Tables two router
- One for EC2 - EC2_ROUTER
- Another for AWS Lambda - AWS_Lambda_ROUTER
- Create an Internet Gateway
- Add 0.0.0.0/0 rule in EC2_ROUTER for target igw-xxxxx
- This will create public subnet and give INTERNET access for EC2
- Create a NATGateway with public subnet which has igw-xxxxx as target
- Create a private subnet which and attach to AWS_Lambda_ROUTER, with target 0.0.0.0/0 as nat-xxxxxxx
- Select VPC for AWS Lambda
- Select a/all private subnet which is private having NAT Gateway for all traffic 0.0.0.0/0
all done, go ahead and enjoy
Note
- 访问 public API 而非 AWS
时需要 NATGateway
- 内部 AWS 服务有 AWS 端点,可以通过创建
在 AWS_Lambda_ROUTER 中附加 AWS 端点
Internet access in AWS Lambda -> Private Subnet with NATGateway -> NATGateway is in public subnet with Internet Gateway which access to AWS Lambda.
PS
I have no Idea why AWS Lambda can not have INTERNET access via InternetGateway why it needs NATGateway
我开发了一个带有明确截图的分步教程:
第一部分
- 在 AWS 中创建 lambda 函数
- 创建一个 API 网关以将所有请求重定向到您的 AWS Lambda 函数
第二部分
- 为您的 lambda 函数配置 VPC
- 以安全的方式授予对 lambda 函数的互联网访问权限
- 将 Node.js Restful API 部署到您的 Lambda 函数中
你可能做错了一件事(这正是我做错的,但从上面的@Ramratan Gupta 那里得到了提示)是将 NAT 网关放在私有子网中而不是 public .
NAT 网关为您的私有 IP Lambda 函数提供互联网连接,因此按理说 NAT 网关需要自己访问互联网并位于 Public 网络中。
总结一下:
您必须至少创建两个子网:一个 public 和一个私有子网。将两者放在同一个 VPC 中。
使子网 public 或私有的是 0.0.0.0/0
路由。
您需要同时拥有互联网网关 (igw) 和 NAT 网关 (ngw)。
要创建 NAT 网关,您需要一个弹性 IP。
此外,我做错了,您需要在 PUBLIC 网络中创建 NAT 网关。
igw 与 NAT 网关位于同一子网中:public 一个。
您需要有两条路由:一条 0.0.0.0/0
路由引用 igw,另一条引用 ngw。
然后您需要将包含路由 table 的 NAT-route 与私有子网相关联,并将包含路由 table 的 igw-route 与 public 子网。请注意,在您执行此关联之前,使一个 public 和一个私有的子网之间没有区别。通过如何将子网与路由 tables.
相关联,可以将一个设为私有,将另一个设为私有 public
最后,您需要将 Lambda 函数连接到 VPC,并且仅连接到私有子网。这样,当您的 lambda 尝试访问 Internet 时,它会转到私有子网,查找 0.0.0.0/0
路由,该路由会将其定向到 public 子网中的 NAT 网关。 NAT 网关然后使用 igw 连接互联网(因为它们在同一子网中)并在收到响应后,returns 将其发送到您的 Lambda 函数。
另请注意,您的 Lambda 函数需要一些权限(AWS 会警告您)才能连接到 VPC。
我正在尝试从 Lambda 函数中向第三方服务发出出站 API 请求,但该函数总是超时且没有任何错误。
这以前发生在尝试在不同的功能(仍在相同的 VPC/子网)中执行 s3.putObject
操作时,我设法通过添加具有服务名称的端点来解决这个问题 com.amazonaws.us-east-1.s3
并将其连接到与此 Lambda 函数所在的 VPC 关联的路由 table。
在网络框 -> 安全组部分的 Lambda 仪表板中,我看到了这个警告:
When you enable VPC, your Lambda function will lose default internet access. If you require external internet access for your function, ensure that your security group allows outbound connections and that your VPC has a NAT gateway.
我相信这个安全组允许出站连接,基于下面的出站规则 table:
对于第二个要求,我可以确认这个 VPC 有一个 NAT 网关,因为在 VPC 仪表板上,在 NAT 网关选项卡中,出现在那里的那个有一个与之关联的 VCP,并且那个 VPC 是同一个托管 Lambda 函数。
我按照指南创建了一个 Flow Log 来监控进出 VPC 的流量,希望看到那些出站请求确实被拒绝了。但是,这样做并检查 CloudWatch 日志后,所有记录都以 ACCEPT OK
或 NODATA
.
How can I grant internet access to my VPC Lambda function? 是我最初尝试遵循的指南,但我卡在了 To create a public or private subnet
:
From the Change to: drop-down menu, choose an appropriate route table: For a private subnet, the default route should point to a NAT gateway or NAT instance:
Destination: 0.0.0.0/0 Target: nat-… (or eni-…) For a public subnet, the default route should point to an internet gateway:
Destination: 0.0.0.0/0 Target: igw-…
对于此 VPC 中的所有四个子网,单击 Change to:
右侧的下拉列表仅显示一个选项,即已选择的选项 rtb-xxxxxxxx
。单击通往该路线 table 的 link 并单击“摘要”旁边的“路线”选项卡后,我看到了:
阻止 Lambda 函数访问 Internet 可能是我做错了什么?
要让 Lambda 通过 VPC 访问互联网,它应该在 Private Subnet
中并附加 NAT Gateway
。
根据您的屏幕截图,附加到子网的路由 table 已附加 igw-xxxxxxx
使您当前的子网成为 public subnet
.
因此,要使事情正常进行,您可以执行以下操作:
- 在您当前
subnet
的路线 table 中附加
NAT Gateway
而不是 igw-xxxxxx
或
- 找到连接到您的 Lambda 的 ENI 并附加
Elastic IP
如果您想要访问互联网。
或
根据@John Rotenstein,如果您的 Lambda 不需要 VPC 资源,您可以将 Lambda 移出 VPC
在 VPC
模式下设置 AWS Lambda 互联网访问很棘手,因为 AWS 方面没有提供适当的 AWS 文档,我确实多次阅读这篇文章 https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/ 但没有观看 YouTube 视频。经过这么多的研发,我可以总结出以下步骤是 在 VPC 模式下设置 AWS Lambda
- Route Tables two router
- One for EC2 - EC2_ROUTER
- Another for AWS Lambda - AWS_Lambda_ROUTER
- Create an Internet Gateway
- Add 0.0.0.0/0 rule in EC2_ROUTER for target igw-xxxxx
- This will create public subnet and give INTERNET access for EC2
- Create a NATGateway with public subnet which has igw-xxxxx as target
- Create a private subnet which and attach to AWS_Lambda_ROUTER, with target 0.0.0.0/0 as nat-xxxxxxx
- Select VPC for AWS Lambda
- Select a/all private subnet which is private having NAT Gateway for all traffic 0.0.0.0/0
all done, go ahead and enjoy
Note
- 访问 public API 而非 AWS 时需要 NATGateway
- 内部 AWS 服务有 AWS 端点,可以通过创建 在 AWS_Lambda_ROUTER 中附加 AWS 端点
Internet access in AWS Lambda -> Private Subnet with NATGateway -> NATGateway is in public subnet with Internet Gateway which access to AWS Lambda.
PS
I have no Idea why AWS Lambda can not have INTERNET access via InternetGateway why it needs NATGateway
我开发了一个带有明确截图的分步教程:
第一部分
- 在 AWS 中创建 lambda 函数
- 创建一个 API 网关以将所有请求重定向到您的 AWS Lambda 函数
第二部分
- 为您的 lambda 函数配置 VPC
- 以安全的方式授予对 lambda 函数的互联网访问权限
- 将 Node.js Restful API 部署到您的 Lambda 函数中
你可能做错了一件事(这正是我做错的,但从上面的@Ramratan Gupta 那里得到了提示)是将 NAT 网关放在私有子网中而不是 public .
NAT 网关为您的私有 IP Lambda 函数提供互联网连接,因此按理说 NAT 网关需要自己访问互联网并位于 Public 网络中。
总结一下:
您必须至少创建两个子网:一个 public 和一个私有子网。将两者放在同一个 VPC 中。
使子网 public 或私有的是
0.0.0.0/0
路由。您需要同时拥有互联网网关 (igw) 和 NAT 网关 (ngw)。
要创建 NAT 网关,您需要一个弹性 IP。
此外,我做错了,您需要在 PUBLIC 网络中创建 NAT 网关。
igw 与 NAT 网关位于同一子网中:public 一个。
您需要有两条路由:一条
0.0.0.0/0
路由引用 igw,另一条引用 ngw。然后您需要将包含路由 table 的 NAT-route 与私有子网相关联,并将包含路由 table 的 igw-route 与 public 子网。请注意,在您执行此关联之前,使一个 public 和一个私有的子网之间没有区别。通过如何将子网与路由 tables.
相关联,可以将一个设为私有,将另一个设为私有 public最后,您需要将 Lambda 函数连接到 VPC,并且仅连接到私有子网。这样,当您的 lambda 尝试访问 Internet 时,它会转到私有子网,查找
0.0.0.0/0
路由,该路由会将其定向到 public 子网中的 NAT 网关。 NAT 网关然后使用 igw 连接互联网(因为它们在同一子网中)并在收到响应后,returns 将其发送到您的 Lambda 函数。另请注意,您的 Lambda 函数需要一些权限(AWS 会警告您)才能连接到 VPC。