2

Following is my Lambda handler which is expecting the users data from the queryStringParameters:-

export const lambdaHandler = async (event, context) => {
    try {
        const numberOfUsersRequested= (event && event.queryStringParameters.users) ? event.queryStringParameters.users : 10;
        const users = await generateUsers(numberOfUsersRequested).then(data => data.users);

I'm using AWS SAM to develop my Lambda and I can test it very well using the event.json as an input event to this Lambda locally. Here is a chunk of event.json where I'm passing the queryStringParamters users like this:-

{
  "body": "{\"message\": \"mock data\"}",
  "resource": "/{proxy+}",
  "path": "/path/to/resource",
  "httpMethod": "POST",
  "isBase64Encoded": false,
  "queryStringParameters": {
    "users": 200
  },

Now, may I know how to pass the same QueryStringParameters from the AWS API Gateway console. Currently, I'm getting this 500 error on the AWS console for the API Gateway:-

{
  "message": "Internal server error"
}

Mon Sep 28 01:24:15 UTC 2020 : Endpoint response headers: {Date=Mon, 28 Sep 2020 01:24:15 GMT, Content-Type=application/json, Content-Length=2, Connection=keep-alive, x-amzn-RequestId=0e1f110c-e80c-4ff1-870a-5cafd04167db, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5f713b3d-4762f9b07ee8c1d7c6623574;sampled=0}
Mon Sep 28 01:24:15 UTC 2020 : Endpoint response body before transformations: {}
Mon Sep 28 01:24:15 UTC 2020 : Execution failed due to configuration error: Output mapping refers to an invalid method response: 200
Mon Sep 28 01:24:15 UTC 2020 : Method completed with status: 500

I have performed the following steps to mitigate the issue but not working. It looks like something is missing :- 1)Added the url query string parameters as users in the Method Request (refer screenshot)

enter image description here

  1. In the Integration Request -> Mapping Templates, added the mapping as application/json:-

    { "users": "$input.params('users')" }

enter image description here

enter image description here

enter image description here

  1. And finally passing the query string as users=6.

1 Answer 1

2

In your case, the event should just be:

{
    "users": "6"
}

You can add the following to the beginning of your handler to confirm:

  console.log(JSON.stringify(event, null, 2));

Therefore, to get users value you should just use event.users, not event.queryStringParameters.users.

Sign up to request clarification or add additional context in comments.

3 Comments

this is a code level change. Do I need to make any change on the AWS console for API Gateway when it comes to passing users and its mapping. Because with latest code deployed (as per your suggestion), I'm still getting 500 error. Any changes to be made when passing the param from API Gateway console? Please advise, thanks
@vinod827 Nothing else should be required from what I can tell. But I think there is some confusion if you worry about code change. Your original event you are testing and developing for is for lambda proxy integration. But your API is for non-proxy integration. I made the answer based on the letter. I think you have to decide which integration to use, as you are using both in your question, and that's why you have these issues. btw, proxy integration is much easier and most commonly people use it for this reason.
@vinod827 Also, did you print out the event using console.log? You can check its form in CloudWatch logs to confirm its real form and content.

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.