如何通过 Amazon API Gateway + Lambda(节点)检索用户的公共 IP 地址 [英] How can I retrieve a user's public IP address via Amazon API Gateway + Lambda (node)

查看:36
本文介绍了如何通过 Amazon API Gateway + Lambda(节点)检索用户的公共 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个 Node.js lambda 函数,我想在其中记录传入请求者的公共 IP 地址.我一整天都在浏览 API Gateway 和 Lambda 文档,但没有找到解决方案.

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 Lambda docs all day, but haven't found a solution.

lambda event 对象是否包含我可以用来提取用户 IP 的请求元数据?

Does the lambda event object include request metadata I can use to extract the user's IP?

推荐答案

这里是在 Lambda 函数中使用 API Gateway 的 $context.identity.sourceIp 的简单演示.

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

API 映射模板:

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

Lambda 函数:

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

这篇关于如何通过 Amazon API Gateway + Lambda(节点)检索用户的公共 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆