AWS Lambda Errors With Java Lambdas and Serverless Framework
Make your lambdas work right with Lambda.
Join the DZone community and get the full member experience.Join For Free
Using a basic Serverless framework event config like this:
...will create an API Gateway config with the Lambda Proxy feature enabled.
This sends the request as a JSON object to the Lambda. This includes HTTP headers, queryStringParameters, pathParameters and the request body. The Lambda is expected to have an appropriate parameter type for the incoming JSON payload otherwise you'll get a Jackson parsing error like this:
Use a Map<String, Object> as shown in the docs and the incoming request JSON will get passed in as this map.
Even if you are calling the Lambda with a GET request and not intending to POST or PUT a request body, you still need to have the Map<String, Object> parameter to receive the JSON event containing all the headers and params passed from API gateway.
If you return a regular String or anything other than the expected response payload, from your Lambda will cause API Gateway to fail parsing the expected JSON response:
With the API Gateway Lambda Proxy feature enabled, API Gateway expects response payloads from your Lambda to match the format shown here in the docs. Serverless framework generates a class called APIGatewayResponse that matAches the expected format. Either create your own POJO that matches the expected return format or use the generated APIGatewayResponse class.
The generated sample Handler class shows this in use:
Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.