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