
Error handling
In an AWS Lambda function written in Java, error handling can be done by using the usual try/catch semantics.
If one requires the exception context to be propogated back to the caller (only in the case of a synchronous execution), the following code shows how Lambda returns the exception context in a JSON format. Notice how the stack trace elements are a part of the JSON array:
{
"errorMessage": "An invalid input was supplied",
"errorType": "java.lang.Exception",
"stackTrace": [
"example.Hello.handler(Hello.java:9)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:497)"
]
}
During synchronous execution, if an error condition is signaled, the preceding error is shown in the console logs.
In the case of asynchronous execution, these are stored in CloudWatch. There are ways to allow for advanced error handling by using AWS state machines, which are out of the scope of this book.
Now that we have provided an overview of the basic concepts of AWS Lambda, let's create and configure a simple Lambda function.