Search code examples
amazon-web-servicesamazon-ec2aws-lambdaelastic-ip

How to choose Elastic IP when my aws lambda function execute


I want to select specific Elastic IP my own when my lambda function executed.

my service has to respond to several situations, and by user's attributes. Could I write code in a lambda function, that can choose specific my own elastic IP?

I had searched for this. but old information says it cannot do. but recently I heard about it is possible by using Network Load Balancer or Application Load Balancer. But I don't know how to use this for the problem.


Solution

  • No. You cannot associate an Elastic IP (EIP) address with an AWS Lambda function.

    Well, actually you can, but I wouldn't recommend it. When a Lambda function is associated with a VPC, it connects via an Elastic Network Interface (ENI). It is possible to attach an EIP to an ENI. This also grants access to the Internet if it is attached to a public subnet.

    So why avoid it? Because Lambda might create additional ENIs, especially if the Lambda function is frequently invoked and run in parallel. This means it will not have a consistent ENI.

    An alternative method is:

    • Attach the AWS Lambda function to a private subnet
    • Put a NAT Gateway in a public subnet
    • Associate an Elastic IP address with the NAT Gateway
    • All traffic from the Lambda function to the Internet will then come from the NAT Gateway's EIP (however, I don't think you can change that EIP)