Getting IP using NodeJS, a Rest API, and Lambda

I have attempted to use the method below to retrieve an IP address from incoming API requests

Here is a simple demonstration of using API Gateway's $context.identity.sourceIp in a Lambda function.

this is the API Mapping template that I use for the integration process being sent to lambda:

{
    "sourceIP" : "$context.identity.sourceIp"
}

This is how I build the lambda function using NodeJS

'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
    console.log('SourceIP =', event.identity.sourceIp);
    callback(null, event.identity.sourceIp);
};

I keep hitting an error for the SourceIp, in the event.identity.sourceIp section.

Response:

{
  "errorType": "TypeError",
  "errorMessage": "Cannot read property 'sourceIp' of undefined",
  "trace": [
    "TypeError: Cannot read property 'sourceIp' of undefined",
    "    at Runtime.exports.handler (/var/task/index.js:4:46)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

I think that event.identity.sourceIp is the wrong array to use, and that I maybe should be using event.sourceIp but I see online people using identity within the code. However when using event.sourceIp I get a null response. this looks like the following

null, under the log I get

START RequestId: 4fe70699-6e2a-411a-b7a5-684d62d4051b Version: $LATEST
2020-11-23T01:50:40.759Z    4fe70699-6e2a-411a-b7a5-684d62d4051b    INFO    SourceIP = undefined
END RequestId: 4fe70699-6e2a-411a-b7a5-684d62d4051b
REPORT RequestId: 4fe70699-6e2a-411a-b7a5-684d62d4051b  Duration: 19.15 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 64 MB  Init Duration: 143.93 ms    

I need to know the following: is event.identity.sourcecIp still good? should I be using event.sourceIp instead?

Also Why am I getting a null response for using event.sourceIP , in the log I see INFO SourceIP = undefined????



Read more here: https://stackoverflow.com/questions/64961663/getting-ip-using-nodejs-a-rest-api-and-lambda

Content Attribution

This content was originally published by Alkisnar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: