2

If one uses "Lambda Proxy Integration" (here too & here) in API Gateway, there are two important changes that occur:

1) data is mapped to event.body (rather than directly on the event object requiring mapping templates)

2) event.body is stringified and needs to be JSON.parse'd

Here's an example of a simple Lambda function that expects input to conform to Lambda Proxy Integration from API Gateway (node10x)--

exports.handler = async (event, context) => {

    if (event && event.body) {
    try {
        event.body = JSON.parse(event.body);
    } catch(e) {
        return {
            statusCode: 200,
            body: JSON.stringify({message: 'Hit error in try/catch'})
        }
    }
}
    return {
        statusCode: 200,
        body: JSON.stringify({message: event})
    }

}

Q: How (or should) one use Lambda's "built-in" test input feature for a Lambda script that's expecting data in the manner from Lambda Proxy Integration?

The test inputs come directly on the event object (not event.body) and the data isn't stringified from the test payload

enter image description here

We can solve #1 if the function is expecting event.body we can pass that in for a test but then the event.body is expected to be stringified. We can silently skip the try/catch but that seems a bit clunky

Is there a better technique to send "test" input data to a Lambda function? Unless I have overlooked something, it seems like you either need to get a better solution or hand-craft the test inputs to "match" the expected payload from an API Gateway invocation w/ Lambda Proxy Integration

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Victor
  • 868
  • 1
  • 9
  • 24
  • 2
    Why don't you put stringified JSON object to "body" key as value in your test event? – Jan Giacomelli Jul 25 '19 at 06:01
  • That crossed my mind as well-- I think the bottom line for now is not to rely too much on "testing" Lambdas that way with Proxy Integration and run locally w/ SAM or other tooling ex: https://github.com/awslabs/serverless-application-model?source=post_page – Victor Jul 25 '19 at 17:33

1 Answers1

0

Instead of trying with default event configuration suggested, have you tried selecting pre-defined test event templates when you try to test your Lambda functions?

You can try Event Template --> API Gateway AWS Proxy.

trnrao
  • 86
  • 1
  • 2