Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - callback("some error type") equivalent in Java 8

How do I make a lambda function report a failure in Java 8?

I see this is possible in Node.js.
http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Using the Callback Parameter
The Node.js runtime v4.3 supports the optional callback parameter. You can use it to explicitly return information back to the caller. The general syntax is:

callback(Error error, Object result);

Where:

  • error – is an optional parameter that you can use to provide results of the failed Lambda function execution. When a Lambda function >succeeds, you can pass null as the first parameter.

  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.

Note

Using the callback parameter is optional. If you don't use the optional callback parameter, the behavior is same as if you called the callback() without any parameters. You can specify the callback in your code to return information to the caller.

If you don't use callback in your code, AWS Lambda will call it implicitly and the return value is null.

When the callback is called (explicitly or implicitly), AWS Lambda continues the Lambda function invocation until the Node.js event loop is empty.

The following are example callbacks:

callback(); // Indicates success but no information returned to the caller. callback(null); // Indicates success but no information returned to the caller. callback(null, "success"); // Indicates success with information returned to the caller. callback(error);
// Indicates error with error information returned to the caller.

AWS Lambda treats any non-null value for the error parameter as a handled exception.

like image 583
user1932634 Avatar asked Aug 02 '16 15:08

user1932634


People also ask

How do you check for errors in Lambda?

To troubleshoot Lambda code errors You can use CloudWatch to view all logs generated by your function's code and identify potential issues. For more information, see Accessing Amazon CloudWatch Logs for AWS Lambda.

Is Java good for AWS Lambda?

You can run Java code in AWS Lambda. Lambda provides runtimes for Java that run your code to process events. Your code runs in an Amazon Linux environment that includes AWS credentials from an AWS Identity and Access Management (IAM) role that you manage. Lambda supports the following Java runtimes.

Is there any way to catch AWS Lambda timed out error in code level?

Verify that your Lambda function is timing outRetrieve the request IDs of any timed-out invocations by searching the function's Amazon CloudWatch log group for the phrase, Task timed out. Then, use the request IDs of the associated timed-out invocations to retrieve the full logs for each invocation timeout.


1 Answers

Just throw an exception and do not catch it anywhere. Any uncatched exception causes Lambda failure. You can see more information about how to report failures in AWS Lambda with Java: https://docs.aws.amazon.com/lambda/latest/dg/java-exceptions.html

public TestResponse handleRequest(TestRequest request, Context context) throws RuntimeException {
   throw new RuntimeException("Error");
}

Note the throws declaration which allows to throw an unhandled exception to out of the method.

like image 175
Çağatay Gürtürk Avatar answered Sep 23 '22 13:09

Çağatay Gürtürk