Is there a way to assign a Static IP to a AWS Lambda without VPC?

infernal_lad picture infernal_lad · Jun 19, 2019 · Viewed 9.8k times · Source

I am looking to assign a static IP to my Lambda which is being invoked via the API gateway. This is required because, the downstream system that i invoke from this lambda accepts web requests only from a Whitelisted IP.

I am successful in achieving this via the VPC that i associate with my lambda. But VPC introduces a bad cold-start time which sometime ranges 12-16seconds. So i am looking for a way to prevent this cold start from the VPC, but at the same time assign a static IP to the lambda.

Answer

John Rotenstein picture John Rotenstein · Jun 20, 2019

You will need to:

  • Create a VPC with an Internet Gateway, a public subnet and a private subnet
  • Attach the AWS Lambda function to the private subnet
  • Launch a NAT Gateway in the public subnet and update the Route Table of the private subnet to use the NAT Gateway

The NAT Gateway will use an Elastic IP address (which is a static IP address). All traffic from the Lambda function to the Internet will come from this IP address, which can be used in the whitelist.

You might think that this is a bit of overkill for simply attaching a static IP address, but multiple Lambda function can run in parallel and they could run in multiple Availability Zones. Sending all traffic through the NAT Gateway is the only way to ensure they all have the same IP address. (Or, to be more specific, one IP address per AZ in which the NAT Gateway is launched.)