How can I get the user's real IP address with Amplify, API Gateway, and Lambda?

June 4, 2025 3 answers
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

CloudflareNerd
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/)

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.

Reply to this answer
TechGuru88
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
CuriousDev74
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.

Related Discussions