How can I assign a static IP address to a Lambda function?
2 minute read
0
I want to assign a static IP address to an AWS Lambda function.
Resolution
Follow these steps to assign a static IP address to a Lambda function.
Step 1: Connect a Lambda function to an Amazon Virtual Private Cloud (Amazon VPC)
Configure your Lambda function to connect to an Amazon VPC. The Lambda function is assigned an elastic network interface (ENI) with a private IP address. The Lambda elastic network interface private IP address can't be assumed as the static IP address because it's changed during the elastic network interface lifecycle.
Note: It's a best practice not to place Lambda functions in an Amazon VPC unless the function must access other resources in the Amazon VPC.
Step 2: Grant internet access to a Lambda function in an Amazon VPC
Internet access from a private subnet requires network address translation (NAT). To give internet access to an Amazon VPC-connected Lambda function, route its outbound traffic to a NAT gateway or NAT instance in a public subnet. Make sure that the NAT gateway or NAT instance has a route to an internet gateway.
Step 3: Associate the NAT gateway or instance with an Elastic IP address
Associate an Elastic IP address with the public NAT gateway or instance. The NAT gateway or instance replaces the source IP address of the instances with the Elastic IP address. This Elastic IP address can be assumed as the static IP address for the Lambda function.
Note:
It's a best practice to create multiple subnets across different Availability Zones. This practice creates redundancy and allows the Lambda service to maintain high availability for your function.
You can't associate an Elastic IP address with a private NAT gateway or instance.
You're limited to associating 2 Elastic IP addresses to your public NAT gateway or instance by default. For more information, see Elastic IP addresses quotas.