Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Object is empty in AWS Lambda nodejs function

I am using Lambda function to query a RDS MySQL database. To fetch a row, I am passing the primary key as parameter in the URL (with AWS API Gateway). Example URL is:

https://aaaaaaa.execute-api.aaaaaaa.amazonaws.com/default/getresult?pk=1245

In the Lambda function,

exports.handler =  (event, context, callback) => {
  //prevent timeout from waiting event loop
  callback(null, event);

};

I am getting output as {} for the url.

Note: Lambda proxy integration is enabled.

like image 449
Mallikarjun M Avatar asked Apr 25 '19 17:04

Mallikarjun M


3 Answers

Lambda Proxy Integration should be enabled on the API Gateway in order for API Gateway to pass the event details, including the params, to Lambda.

See this image here for how to do this in the console:

enter image description here

Go to your API, then your Resources, then your Method Execution, and then select "Integration Request". From there tick the box that says "Use Lambda Proxy Integration".

Enabling this allows API Gateway to proxy the request to Lambda with the request details, including the params, available in the event.

like image 58
hephalump Avatar answered Oct 20 '22 07:10

hephalump


Make sure, you check the Use Lambda Proxy Integration check box, which will establish an integration of type Lambda-Proxy between API Gateway’s method and the associated Lambda function.

With the Lambda proxy integration, Lambda is required to return an output of the following format (doc):

{
  "isBase64Encoded" : "boolean",
  "statusCode": "number",
  "headers": { ... },
  "body": "JSON string"
}

This mean if you want to send back event object to client, you have to put to callback a object with above format.

 exports.handler = (event, context, callback) => {
  //prevent timeout from waiting event loop
  const response = {
    statusCode: 200,
    headers: {
      "x-custom-header": "my custom header value"
    },
    body: JSON.stringify({
      message: 'Your function executed successfully!',
      input: event,
    }),
  };

  // success response
  callback(null, response);

};
like image 28
hoangdv Avatar answered Oct 20 '22 06:10

hoangdv


This problem can also occur if you go most of the way through the API Gateway setup modification process but do not click on Deploy. Until we clicked on Deploy, we got test requests populated with event information, while real HTTPS connections created empty events ({}).

like image 2
Eric Boesch Avatar answered Oct 20 '22 06:10

Eric Boesch