Nodejs API call returning undefined to lambda function

Assumptions:

  • You're using Lambda-Proxy Integration.
  • You want to pass the the exact same payload that the first Lambda received to the second Lambda.*

You're misunderstanding what event is. This is NOT the JSON payload that you sent through your HTTP request.

An HTTP request through the API Gateway gets transformed into an event object similar to this:

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}

As you can see, the JSON payload is accessible in a stringified form in event.body.

If you want to send the pass the same payload to the second Lambda, you have to parse it first.

const body = JSON.parse(event.body)

Then, send body instead of event.

Then, in your second Lambda, you parse the stringified JSON in event.body and then you get your original payload back.

If you sent name in that original payload, you can get it from JSON.parse(event.body).name.

Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format


Had a similar problem and debugged with logging event to console.

Add logging on the event,

console.log(JSON.stringify(event));

to evaluate how mapping is done in your API-Gateway to Lambda integration and see where the post parameter exists.

If the post value is not there fix the integration until you get the post values in your event.

Data Mapping API-Gateway to Lambda:

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

Hope it helps.