1

I'm using Go on AWS lambda and I'm writing the following code:

func HanldeLambdaFunction(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {
    limitString := request.QueryStringParameters["limit"]
    fmt.Println("limitString", limitString) //nothing is written
}

I'm testing the lambda function using the api gateway and providing params:

limit=2

The lambda function is being executed successfully but the limit is not being read.

How can I access the params?

1
  • How are you making the request to API Gateway? Pls include an example of the request + method. Commented Nov 18, 2020 at 17:31

1 Answer 1

2

The problem with the code you posted is, that it should not even compile. The biggest problem being, that there is no Response struct. It probably should be events.APIGatewayProxyResponse.

Furthermore, the code does not return anything, even though you define that it should return Response and error.

I took your code and fixed all of this and it works for me. The fixed code looks like this:

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func HanldeLambdaFunction(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    limitString := request.QueryStringParameters["limit"]
    fmt.Println("limitString", limitString) //nothing is written

    return events.APIGatewayProxyResponse{StatusCode: 200}, nil
}

func main() {
    lambda.Start(HanldeLambdaFunction)
}

The output is:

START RequestId: 0c63f94f-b0aa-49de-ba6d-b1150d711b8a Version: $LATEST
limitString 2
END RequestId: 0c63f94f-b0aa-49de-ba6d-b1150d711b8a
REPORT RequestId: 0c63f94f-b0aa-49de-ba6d-b1150d711b8a  Duration: 0.56 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 34 MB

If I had to guess, then your code does not even run. If it would run but could not read the limit parameter, it should at least print limitString.

Remember, if you compile a go binary for AWS Lambda, you need to compile it for linux and amd64 like so:

$ GOOS=linux GOARCH=amd64 go build
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jens, your answer is surely helpful, but that didn't work! I guess that the problem is in the API Gateway configuration. Thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.