Serverless-express: Unable to get remote source IP

Created on 28 Sep 2018  路  2Comments  路  Source: vendia/serverless-express

In Express we can do :

var sourceIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

But its not working in serverless-express.

Also tried with :

const sourceIp = req.requestContext.identity.sourceIp;

Since serverless provides this with and it works in lambda functions

const sourceIp = event.requestContext.identity.sourceIp;

Most helpful comment

For Future references :

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
router.use(awsServerlessExpressMiddleware.eventContext());

usage :

const sourceIp =req.apiGateway.event.requestContext.identity.sourceIp

All 2 comments

Check out the example in this repo which includes usage of middle Ware to get the context.

For Future references :

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');
router.use(awsServerlessExpressMiddleware.eventContext());

usage :

const sourceIp =req.apiGateway.event.requestContext.identity.sourceIp

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sclark39 picture sclark39  路  7Comments

donny08 picture donny08  路  8Comments

brikis98 picture brikis98  路  9Comments

dougmoscrop picture dougmoscrop  路  5Comments

theweaklink picture theweaklink  路  3Comments