title: Get user's IP Address in Lambda (with API Gateway, and Python)
body: <p>I was using this technique (<a href="https://stackoverflow.com/questions/48619163/how-could-i-retrieve-aws-lambda-public-ip-address-by-using-python/51216578">How could I retrieve AWS Lambda public IP address by using Python?</a>) but it gives the IPAddress of the Lambda Server within AWS.</p>
  <p>Based on this: <a href="https://stackoverflow.com/questions/33062097/how-can-i-retrieve-a-users-public-ip-address-via-amazon-api-gateway-lambda-n">How can I retrieve a user&#39;s public IP address via Amazon API Gateway + Lambda (node)</a>, it looks like I should be able to use</p>
  <pre><code>ip_address = event['requestContext']['identity']['sourceIp'];
  </code></pre>
  <p>My handler starts like this:</p>
  <pre><code>def lambda_handler(event, context):
  </code></pre>
  <p>but if I do a <code>pprint.pprint(event)</code>, I don't see any RequestContext in it, only the &quot;body&quot;.</p>
  <p>The last comment by FFXSam on the Jonathan answer says &quot;It should be noted that event.requestContext.identity is not present if you're serving a page that's not behind an authorizer.&quot;.
  I'm not sure what that means or why it is true.  I'm using API Gateway, and JavaScript code on the client side is calling it.</p>
  <p>I could ask the client coder to send me the local IP Address in the body, but it seems like I should be able to get it in the Lambda function itself.</p>
  <p>Someone ask for the events, even though I said it only had the fields being passed in a json element called &quot;body&quot;:</p>
  <p>code:</p>
  <pre><code>print(&quot;pprint event:&quot;)
  pprint.pprint(event)
  
  
  2021-06-06T13:30:01.231-05:00   pprint event:
  2021-06-06T13:30:01.231-05:00   {'body': {'ResponseTimeMilliseconds': 2225,
  2021-06-06T13:30:01.231-05:00   'authToken': '12312312',
  2021-06-06T13:30:01.231-05:00   'handNumber': 7}}
  </code></pre>