[英]Getting requester's IP address in AWS lambda function
I understand, from the answer to this question , that I can get the IP address of the caller of a node.js lambda function by doing this:我明白,从这个问题的答案中,我可以通过这样做获得 node.js lambda function 的调用者的 IP 地址:
events['headers']['X-Forwarded-For']
It seems like, for Go, this information should be inside the context.Context
for lambda signatures that take it.看起来,对于 Go,此信息应该在
context.Context
中,用于 lambda 签名。 However, looking at the documentation , I don't see any mention of request headers.但是,查看文档,我没有看到任何关于请求标头的提及。 Is there a way to get the same information in a Go lambda function?
有没有办法在 Go lambda function 中获取相同的信息?
After doing some research on my own, I figured out that I needed to put my lambda function behind an API Gateway instance to get the information I was interested in. After doing that, I could modify the handler like this:在我自己做了一些研究之后,我发现我需要把我的 lambda function 放在一个 API 网关实例后面来获取我感兴趣的信息。这样做之后,我可以像这样修改处理程序:
package main
import (
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func LambdaHandler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (int, error) {
// some code
addr := request.RequestContext.HTTP.SourceIP
// some other code
return 0
}
func main() {
lambda.Start(LambdaHandler)
}
By doing this, the API gateway will populate all the request metadata I need and pass that on to my lambda function. The request body is now contained in request.Body
so I can extract that data using JSON deserialization or whatever other method my data is encoded as.通过这样做,API 网关将填充我需要的所有请求元数据并将其传递给我的 lambda function。请求正文现在包含在
request.Body
中,因此我可以使用 JSON 反序列化或我的数据的任何其他方法提取该数据编码为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.