首页 > 解决方案 > AWS Lambda Function with VPC only works when in Private Subnet

问题描述

I have been working on integrating an Amazon Lambda function with connection to a RDS for the mySQL DB and an external API. To access the API, there needed to be an internet gateway and then security groups that allowed connection from 0.0.0.0/0.

I have a a public subnet and private subnet. The public subnet routes to the internet gateway but the private subnet routes to a NAT.

This lead me to think that if I ran the Lambda function with the Public subnet, it would connect to the internet. However, every time it timed out. But, when I ran the lambda function from within the private subnet, it worked! So it the NAT seems to work since that is what the private sunet was associated with, but just using the internet gateway does not work.

Does anyone have any explanation for this?

标签: amazon-web-servicesaws-lambdanatsubnetvpc

解决方案


要使 Amazon Lambda 函数连接到 Internet,需要满足以下条件之一:

  • Lambda 函数连接到 VPC,或者
  • Lambda 函数连接到私有子网并且配置了 NAT 网关/NAT 实例,或者
  • Lambda 函数连接到公有子网,弹性 IP 地址分配给子网中 Lambda 函数使用的弹性网络接口 (ENI)

仅将 Lambda 函数连接到公共子网(没有 EIP)不会提供 Internet 访问。


推荐阅读