如何通过Amazon API Gateway + Lambda(节点)检索用户的公共IP地址 [英] How can I retrieve a user's public IP address via Amazon API Gateway + Lambda (node)
本文介绍了如何通过Amazon API Gateway + Lambda(节点)检索用户的公共IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我当前正在编写一个Node.js lambda函数,我想在其中记录传入请求者的公共IP地址.我整天都在浏览API网关和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网关的$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屋!
查看全文