AWS lambda功能无法访问互联网

问题描述 投票:3回答:3

我正在运行一个lambda函数,我想访问私有数据库服务器和Internet。我可以很好地访问数据库,但我无法访问互联网。

设置:

VPC (10.0.0.0/16)
   Public-Subnet (10.0.0.0/24)
      NAT-Security-Group (see security groups below)
         NAT-Server (AMI NAT instance)

   Private-Subnet-1 (10.0.1.0/24) & Private-Subnet-2 (10.0.2.0/24)
      DB-Security-Group (see security groups below)
         DB-Server (RDS PostgreSQL instance)

      Lambda-Security-Group (see security groups below)
         Lambda-Function

安全组是:

NAT-Security-Group
   Inbound:
      HTTP & HTTPS from source: Lambda-Security-Group
      SSH from 0.0.0.0/0
   Outbound:
      All traffic

DB-Security-Group
   Inbound:
      PostgreSQL from source: Lambda-Security-Group
   Outbound:
      All traffic

Lambda-Security-Group
   Inbound:
      HTTP & HTTPS from source: NAT-Security-Group
   Outbound:
      All traffic

子网的路由表是:

Public-Subnet:
   10.0.0.0/16 local
   0.0.0.0/0 Internet-Gateway

Private-Subnet-1 & Private-Subnet-2
   10.0.0.0/16 local
   0.0.0.0/0 NAT-Server

我在这里不知所措。为什么lambda函数不能到达互联网(连接超时错误)?

python amazon-web-services networking aws-lambda vpc
3个回答
3
投票

您需要在公有子网中创建NAT网关,并通过NAT网关路由来自放置Lambda的子网的出口流量。

要执行此操作,请将NAT网关设置为路由表中的默认网关,该网关连接到放置Lambda的子网。

有关更多详细信息,请参阅文档中的Internet Access for Lambda Functions


1
投票

公共子网中的lambda

由于您只需要从lambda传递数据库,因此将lambda放入公有子网并且您不需要安装NAT网关。无论如何,不​​会像ELB那样直接访问lambda,并且在通过API端点进行任何访问时必须连接到API网关。

私有子网中的lambda

  • 将NAT路由添加到与所有地址0.0.0.0/0的私有子网关联的路由表中
  • 将所有0.0.0.0/0的IGW路由添加到与公有子网关联的路由表中。
  • 将NAT放在公共子网中

这应该解决从lambda访问互联网的问题。但是,如果您使用EC2中安装的数据库以用于将来的补丁管理或来自堡垒主机的任何其他类型的访问,这只是非常有用。如果使用RDS,就没有必要将lambda放在私有子网中。


1
投票

问题在于安全组的入站/出站规则。通过上面的配置,我更新了安全组以匹配:

NAT-Security-Group
   Inbound:
      HTTP & HTTPS from source: Lambda-Security-Group
      SSH from source: 0.0.0.0/0
   Outbound:
      HTTP & HTTPS to destination: 0.0.0.0/0

DB-Security-Group
   Inbound:
      PostgreSQL from source: Lambda-Security-Group
   Outbound:
      None

Lambda-Security-Group
   Inbound:
      None
   Outbound:
      HTTP & HTTP to destination: NAT-Security-Group
      PostgreSQL to source: DB-Security-Group

Lambda功能现在具有互联网连接功能。

© www.soinside.com 2019 - 2024. All rights reserved.