8

Is it possible to place an aws lambda in a public subnet and thus avoid paying for NAT?

The docs state the following

If your Lambda function needs Internet access, do not attach it to a public subnet or to a private subnet without Internet access. Instead, attach it only to private subnets with Internet access through a NAT instance or an Amazon VPC NAT gateway.

But do not explain why we can not attach the lambda to a public subnet.

Ulad Kasach
  • 181
  • 1
  • 4
  • Because aws doesn't want to directly expose their instance running your code...(That's my guess) – Tensibai Sep 11 '18 at 13:48
  • 1
    A cheaper alternative to the $35/mo NAT Gateway is to create an NAT instance: https://aws.amazon.com/premiumsupport/knowledge-center/vpc-nat-instance/ – Ulad Kasach Sep 11 '18 at 22:45

2 Answers2

5

No, it's not possible to place an AWS Lambda in a public subnet and thus avoid paying for NAT. Lambda functions cannot have public IPs so they cannot route to the internet without a NAT (gateway or instance).

Put them in a private subnet, ensure the private subnet's default route is a NAT in a public subnet, that the NAT has a public IP, and that the VPC has an IGW.

For more, see this Stack Overflow question.

jarmod
  • 151
  • 1
  • 1
2

Provided the Public Subnet where you're launching your Lambda is configured with Auto-assign public IPv4 address: Yes it should work in public a subnet without NAT.

Whether or not it's a good idea is another question. In general you shouldn't launch any backend services in the public subnets, the public subnets are for things like API Gateways, NAT Gateways, Load Balancers, etc. Not for launching the actual backend systems. That's the best security practice.

However that aside I'm pretty confident that with auto-assign IPv4 address enabled Lambdas will work even in the public subnet.

Unless you want the Lambda to connect to any VPC-internal resources (e.g. to your RDS) you may just as well launch it without VPC config (leave those VPC and Subnet settings empty). That will give it access to the internet without having to worry about NAT or public IPs.

Hope that helps

MLu
  • 1,011
  • 5
  • 7