How can I get the user's real IP address with Amplify, API Gateway, and Lambda?
Asked by
CreativeNinja99
I'm working with a setup that involves Amplify, API Gateway, and Lambda. My Amplify app calls the API Gateway, which then triggers a Lambda function. Both Amplify and API Gateway are proxied through Cloudflare, but I'm having trouble getting the real user IP address in my Lambda logs. Instead, I only see a single, consistent IP address. I've checked the context and the event that API Gateway sends to Lambda, as well as the headers Cloudflare sets, but nothing seems to indicate the actual user IP. What could be causing this issue?
Answers
Have you checked out this article? It might be a configuration issue on Cloudflare's end. Hereโs the link: [Restoring Original Visitor IPs](https://developers.cloudflare.com/support/troubleshooting/restoring-visitor-ips/restoring-original-visitor-ips/)
Reply to this answer
Donโt forget to check the API Gateway logs! Sometimes the IP you're seeing is actually coming from Cloudflare. It helps to verify what's being logged there.
CreativeNinja99: Thanks! That made me realize server-side calls from Amplify were showing up instead of my IP.
Reply to this answer
You might want to look at the `X-Forwarded-For` header of the incoming requests; thatโs where the original user IP is usually stored. It often contains a list of IPs, so make sure you're checking the right one!
HelpfulCoder21: I found two IPs there too, but they werenโt my actual IP. Still worth checking!
Reply to this answer
Add your answer
Answers are reviewed before publication.
CreativeNinja99: I did check that article! Initially, it didnโt seem to help because everything looked fine, but I realized it was my own mistake after all.