使用lambda如何获取客户端ip?

0

【以下的问题经过翻译处理】 我当前在构建1个类似于 http://checkip.amazonaws.com/ 的app。 当前我找到的一个 https://stackoverflow.com/questions/33062097/how-can-i-retrieve-a-users-public-ip-address-via-amazon-api-gateway-lambda-n

当我将此用于lambda函数时-

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

和用于映射模板的-

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

我得到以下错误 { "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)" ] }

profile picture
专家
已提问 5 个月前47 查看次数
1 回答
0

【以下的回答经过翻译处理】 这取决于你正在使用哪个API。如果你正在使用HTTP API,源IP位于event.requestContext.http.sourceIPevent.headers.x-forwarded-for。如果你正在使用REST API,它将在event.headers.X-Forwarded-Forevent.requestContext.identity.sourceIp中。你当前获取的是event.identity.sourceIP

注意,在这两种情况下,你不需要使用映射模板,只需从Lambda函数返回一个body。如果你正在使用REST API,你应该可以通过使用模拟集成和映射模板来实现。

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容