Lambda Handler
lambda_handler is a Rust library for creating AWS Lambda functions which handle
multiple requests, routing each based on the type of the request and its name (for
instance, an S3 event name, an SNS topic name, etc.).
It makes for extremely simple code:
use ;
use Serialize;
use ;
use LambdaHandler;
async
async
async
async
Explanation
The code begins with our imports; we need the lambda_runtime for running, and serde
for serializing our response data. From lambda _route::events we import three event
types; these are re-exported from the aws_lambda_events create, but we have to use
them here because we add a trait for internal use.
The Response struct is just what we have decided on here; there are three requirements
here:
- It must implement
DebugandSerialize(which are easily derived, as here) - It must be used in the return type of all handling functions
- It is used to create a
LambdaHandlerinstance which expects handling functions to return it, withLambdaHandler::<Response>::new()
Next we define three async functions that handle the events themselves; this is where
most of the code in your binary actually resides. Each takes an event of type
LambdaEvent<T>, where T is the type of event it handles. And it returns a
Result<Response, LambdaError>.
Finally, we have our async main function. We set up tracing, and then create our
router:
let handler = new
.route
.route
.route
.route;
This adds the handlers to the router with the correct event names. Note that we can handle more than one event with the same handler, if we'd like.