Why would I want to only keep the body content?
One of the lambdas (let's call it X) in our project is being invoked by another lambda, and this is the only organic way we'd like X to be executed.
Input passed to X through its lambda invoker:
{
"foo" : "foo_value",
"bar" : "bar_value"
}
-- Writing general integration tests, we can simply invoke X with the expected event, and it works great.
-- However, for our acceptance tests, we need to invoke X in isolation from AWS rather than locally, and this can only be done via API Gateway
-- ==> so we created a POST event source for X that will only be used for test purposes.
The event sent by API Gateway to X:
{
headers : {
"some-headers1": "some-value1",
....
"some-headersn": "some-valuen",
},
body : {
"foo" : "foo_value",
"bar" : "bar_value"
},
.....
.....
}
The problem that we're looking to solve:
Is there a way to convert the API input to exactly what we're expecting from the lambda invoker?
In other words, Is it possible to write a custom request integration mapping template for the POST endpoint in order to only keep event.body ?
And by that I mean : $util.escapeJavaScript($input.json('$')) and not { body: $util.escapeJavaScript($input.json('$')) }
Why?
We can simply add a few lines of code into X to ignore the extra information generated by the API Gateway, but since API Gateway is not part of the organic execution events, we don't wish to alter X to transform the received event.
How would it be possible?
I would suggest you decouple your lambda function logic, so it can be invoked from multiple services. Basically, you would create a separate logic in your handler for each service that invokes your lambda function to massage the provided event. So the business logic in your lambda function became resource agnostic and always gets the expected input, that was previously massages by the corresponding handler condition. For the inspiration check the following article regarding code structure https://medium.com/@mitch.zorze/2-years-with-aws-lambda-f835bfedec9f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With