Mapping for an API Gateway to a Lambda is important to make sure parameters are properly passed
First let's take a look at the data our Lambda is expecting:
{
"Animal": "Cat",
"Color": "Blue"
}
Now, we can take a look at how to Map a GET API to these parameters
Under the Integration Request of the API, make sure the "Use Lambda Proxy integration" checkbox is Unchecked.
Now, under Body Mapping Templates at the bottom,
add a new Mapping Template with "When there are no templates defined (recommended)"
as the selected pass-through functionality and application/json as the Content-Type.
Then, under the template put the following:
{
"Animal": "$input.params('Animal')",
"Color": "$input.params('Color')"
}
This means that anyone calling your API will be able to use ?Animal=Cat&Color=Blue to pass parameters to your Lambda function.
A POST is largely the same as a GET method, so follow that example up until we build up the template.
For a POST, the template should look like this:
$input.body
I know this looks silly, but that's really it. Because a POST comes with a body, as long as that body is in the right format, we can pass it directly to the Lambda function. Now if this body doesn't match, then we have to get a little more creative.
Calling the API simply means that you would include the following in the body to supply parameters directly to the Lambda function:
{
"Animal": "Cat",
"Color": "Blue"
}