AWS NodeJS Lambda - Get Client IPV4 Address from Lambda Event Object

75 Views Asked by At

I am trying to find the client IPV4 address in the Lambda Event Object. I am using Lambda function URL and lambda is not integrated with API Gateway.

I managed to get it at 2 places -

  1. event.requestContext.http.sourceIp
  2. event.headers["x-forwarded-for"]

I make use of both these fields and if I find the IP address in any of the fields, I use it as client IP address (in case x-forwarded-for has multiple IPs, I use the first IP).

However, after deploying the lambda, I noticed that I am receiving the IP address as 2a02:c7c:e61d:8a00:7fd6:2525:b048:3089 which is in a different format that what I was expecting.

If I debug the same code using usual HTTP Server, I get the usual IPV4 address in HTTP Request.

I tried using node packages like ipaddr.js & ipify but none of them are solving my problem.

Am I missing anything here? Is there a way to convert the above IP address into IPV4 format?

PS - I found this free API https://api.ipify.org/ which allows me to get the client IP on client side however it doesn't suit my requirements as I need to perform this on the server side.

Please let me know if more details are required.

0

There are 0 best solutions below