title: Pass User-Agent and client ip to Lambda Function
body: <p>Kind of fresh to this: </p>
  
  <p>I have a very simple Lambda function that is trying to do a look up against a database with the client/user ip address. I am not passing the IP address from the client but want to pull it from the Request headers of the API Gateway call. </p>
  
  <p>For the POST method of the Resource I am mapping these HTTP Request Headers:</p>
  
  <ul>
  <li>HTTP_CLIENT_IP (not sure if this is sufficient) </li>
  <li>User_Agent</li>
  </ul>
  
  <p>I have one Body Mapping Template for "application/json" for the Integration Request:</p>
  
  <p><code>{ 
  "source_ip" : "$context.identity.sourceIp",
  "user_agent" : "$context.identity.userAgent"
  </code>}</p>
  
  <p>When testing the POST method from the AWS API Gateway console I set the HTTP_CLIENT_IP to a valid private address and the User-Agent to "test". These are my results:</p>
  
  <p>From PostMan my results are: </p>
  
  <p>"<code>{\"isBase64Encoded\":false,\"statusCode\":200,\"headers\":</code>{\"Access-Control - Allow - Origin\":\"*\"<code>},\"body\":</code>{\"badIP\":false,\"ipIsNull\":true,\"ip\":null<code>}</code>}"</p>
  
  <p>The results from the AWS console are not really a concern but the results from PostMan are!</p>
  
  <p>Any help will be appreciated, Thanks. </p>
  
  <p>Below is the very small Lambda function:</p>
  
  <pre><code>"use strict";
  
  console.log('Loading function');
  exports.handler = (event, context, callback) =&gt; `{
       var loc = getGeoLocByIP(event.source_ip, event.user_agent, context);
       callback(null, JSON.stringify(loc));
  `};
  
  function getGeoLocByIP(clientIp, userAgent, event) `{
      var maxmind = require('maxmind');
      if (maxmind.validate(clientIp)) `{
         var fs = require('fs');
         var dbLoc = 'data/GeoLite2-City.mmdb'; 
  
         if (fs.existsSync(dbLoc)) `{
             var cityLookup = maxmind.openSync(dbLoc);
             var lkupres = cityLookup.get(clientIp);
             cityName = lkupres.city.names.en;
             var response = `{
                "isBase64Encoded": false,
                "statusCode": 200,
                "headers": `{
                   "Access-Control - Allow - Origin": "*"
                `},
                "body": `{
                   "cityName": cityName,
                   "client_ip": clientIP,
                   "user_agent": userAgent
                `}
             `};
             return response;
         `} else `{
          console.warn('db file does not exist: ' + dbLoc);
         `}
      `} else `{
        if (clientIp != null) `{
            var response = `{
              "isBase64Encoded": false,
              "statusCode": 200,
              "headers": `{
                  "Access-Control - Allow - Origin": "*"
              `},
              "body": `{
                  "badIP": true,
                  "ipIsNull": false,
                  "ip": clientIp
              `}
          `};
      `} else `{
          var response = `{
              "isBase64Encoded": false,
              "statusCode": 200,
              "headers": `{
                  "Access-Control - Allow - Origin": "*"
              `},
              "body": `{
                  "badIP": false,
                  "ipIsNull": true,
                  "ip" : null
              `}
          `};
      `}
      return response;
  `}
  </code></pre>
  
  <p>`}
  }</p>