

AWS Lambda errors with Java Lambdas and Serverless framework
source link: https://www.kevinhooke.com/2021/01/13/aws-lambda-errors-with-java-lambdas-and-serverless-framework/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AWS Lambda errors with Java Lambdas and Serverless framework
Using a basic Servlerless framework event config like this:
functions: hello: handler: kh.javalambda.HelloLambda::myHandler events: - http: path: hello method: get
…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:
An error occurred during JSON parsing: java.lang.RuntimeException java.lang.RuntimeException: An error occurred during JSON parsing Caused by: java.io.UncheckedIOException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: lambdainternal.util.NativeMemoryAsInputStream@1bce4f0a; line: 1, column: 1] Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: lambdainternal.util.NativeMemoryAsInputStream@1bce4f0a; line: 1, column: 1
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.
Return Type
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:
Wed Jan 13 07:34:32 UTC 2021 : Endpoint response body before transformations: "HelloLambda: hello null" Wed Jan 13 07:34:32 UTC 2021 : Execution failed due to configuration error: Malformed Lambda proxy response Wed Jan 13 07:34:32 UTC 2021 : Method completed with status: 502
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 matches 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:
return ApiGatewayResponse.builder() .setStatusCode(200) .setObjectBody(responseBody) .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless")) .build();
Recommend
-
53
AWS Lambda + Serverless Framework + Python— Part 1 : A Step By Step “Hello World” I am creating a series of blog posts to help you develop, deploy and run (mostly) Python applications on AWS Lambda us...
-
80
In this post, we look at how we enable Pulumi to build and deploy serverless functions in AWS, Azure, or GCP using lambda functions in JavaScript and TypeScript.
-
59
TyX Core Framework Serverless back-end framework in TypeScript for AWS Lambda. Declarative dependency injection and event binding. Table of Contents 5. Service Decorators ...
-
6
Serverless framework deploy for Java AWS Lambdas The Serverless framework ‘serverless deploy’ deploys whatever .jar you have packaged and ready to go within your source project. If you make local code changes but d...
-
6
Serverless Framework: AWS Lambdas with scheduled events and parameters To configure an AWS Lambda to get triggered by a CloudWatch event, you can use the ‘schedule’ event in your config: functions: your-functio...
-
7
‘serverless invoke local’ with Java Lambdas If you create a Java Lambda with the provided template, you’re probably returning a response using the provided ApiGatewayResponseClass. If you run your Lambda locally with ‘server...
-
5
Deploying changes to individual Lambdas using Serverless Framework I have a serverless project that deploys 2 Lambdas in the same stack: service: example-apis2 provider: name: aws memorySize: 512 region: u...
-
6
Serverless Development: Addressing AWS Lambda-related Python ErrorsServerless Development: Addressing AWS Lambda-related Python ErrorsNovember 2nd 2021 new st...
-
10
Using Serverless Framework to build and deploy Docker images for AWS Lambdas AWS Lambdas can be packaged and deployed using a Docker image, described in the docs
-
12
.NET 6 Lambdas on ARM64 - Part 2, ServerlessDownload full source code.This is...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK