After, let’s look at exactly what is being returned by your Lambda function. In order for it to be processed and forwarded on by the API gateway, it needs to look a certain way.
The returned object must have a statusCode
, body
, and headers
attribute. In my example above, I included Content-Type
in the headers
object, but it can be empty if you’d like. body
’s value must be a string
, if we were to pass in the user object here without turning it into a JSON-encoded string, this would fail.
If your return object doesn’t have these attributes, when you test the Lambda-API Gateway connection an error like this will pop up:
As you can see, the error is a Malformed Lambda proxy response
. By making sure that your Lambda function returns the correct format, you’ll avoid these pesky internal server errors.
One more important note, this return format only matters when using the API Gateway-Lambda combination in proxy mode. If you do not check the proxy option, then you can return whatever you’d like, the API gateway will forward it on.
Hopefully this helps you save some time! Again, feel free to contact me or email me at phouse512 at gmail.com if you can’t figure it out.