简体   繁体   中英

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.

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:

在此处输入图像描述

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.

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);

};

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 ({}).

You can also go to the API gateway > resources > {the method you want to use query string parameters} > method request > query string parameters.

And define the parameters there. This gives you control over whether they are required as well.

Then in your lambda function it will be within a 'queryStringParameters' object as well.

So instead of event['val1'] it will be event['queryStringParameters']['val1'] .

Perhaps this is new to the AWS lambda/API gateway setup. But I think it is a slightly more intuitive approach because you are defining the parameters rather than accepting anything and having to guess in the lambda function what the user is sending.

In my case, I typed console log in the lambda function but forgot to deploy that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM