0

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:

events['headers']['X-Forwarded-For']

It seems like, for Go, this information should be inside the context.Context for lambda signatures that take it. 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?

Woody1193
  • 7,252
  • 5
  • 40
  • 90

1 Answers1

0

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:

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.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • 1
    You can use API Gateway for more elaborate use cases like if you have multiple Lambdas or want to use auth on the gateway. However, in simple cases, you might get away with just a [function URL](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html). – lxg Jan 31 '23 at 09:12
  • @lxg The problem is that the context didn't have the caller IP address so I needed to include that information in the request payload. – Woody1193 Feb 01 '23 at 01:35