How can I retrieve a user's public IP address via Amazon API Gateway + Lambda (node)

前端 未结 5 1104
暗喜
暗喜 2020-12-13 05:52

I\'m currently writing a Node.js lambda function, in which I want to log the incoming requester\'s public IP address. I\'ve been looking through both the API Gateway and Lam

5条回答
  •  清歌不尽
    2020-12-13 06:45

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

    API Mapping template:

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

    Lambda function:

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

提交回复
热议问题