Core Development Concepts > Basics
Routes and Events
You will learn about new handlers for routes and events, and how to add new routes and event definitions.
This feature is available since Webiny v5.39.0.
- How routing works.
- How to add new events.
- How to add new routes.
About
In the 5.31.0 version of Webiny we have refactored our handler package (@webiny/handler) to use fastify.
Fastify has a quite extensive documentation
, so feel free to check it out if you need to modify our default behavior.
Fastify enables us to:
- add the possibility to add new routes
and event handling
- consistent
requestand
reply(formerly response in our system) methods throughout the system
- a lot of request lifecycle events
for users to hook into
- implement some other cloud service, at some point, more easily
The @webiny/handler package does not differentiate between incoming Lambda requests, that is left to the @webiny/handler-aws package, which has a possibility to handle few types of events:
- API Gateway Event
- EventBridge Event
- SQS Event
- SNS Event
- S3 Event
- DynamoDB Stream Event
- Raw Event - this one is different from the others, we will go into details in a separate section
The MaincreateHandlerMethod
This method is a simple one, as its only job is to choose the specific event handler to be used based on the event it receives.
There is only one createHandler to be used and it can be imported from @webiny/handler-aws.
This createHandler method will handle all incoming events, except the Raw event, which is handled by the createRawHandler method, imported from the @webiny/handler-aws or createHandler imported from @webiny/handler-aws/raw.
Event Specific Handlers
The underlying event handlers, which the main createHandler chooses depending on the event type, actually initialize the Webiny system.
They will initialize fastify, context and everything which is defined by plugins that were passed on.
There are multiple event handlers shipped with our system, but if we missed something that you need - and is an AWS event type, feel free to open an issue or a PR.
API Gateway Route (Event)
This handler uses @fastify/aws-lambda in the background to handle API Gateway events. It transforms APIGatewayEvent
into the request which
fastify understands, and it is then used throughout the system.
For this handler to work it requires at least one RoutePlugin to be initialized. It always returns LambdaResponse
defined in the
@fastify/aws-lambda package.
Just note that the Route is actually an event, we just call it a route because it is a API Gateway route.
Raw Handler, Event Handler and Event
This handler uses a nice fastify feature that allows you to run any route you have previously defined. So basically, on initialization of the handler we add some dummy route (something like webiny-raw-event) and then run it via the .inject() method on fastify instance.
This is the same procedure being used in the @fastify/aws-lambda package.
The difference is that our Raw handler can return either APIGatewayProxyResult or anything else that is directly sent from the EventPlugin
.
S3 Event
EventBridge Event
SQS Event
SNS Event
DynamoDB Event
Adding New Routes
Adding new routes is quite simple, but you need to add them via both Pulumi code and the RoutePlugin.
The Pulumi code goes into apps/api/webiny.application.ts. You must add the new route there, check out the example below, where we are adding a [POST]/webiny route.
Next thing you need to do is to add it into the apps/api/graphql/src/index.ts file via the RoutePlugin or createApiGatewayRoute method:
Adding Event Handler
Adding event handlers is even simpler than adding routes. You just need to add the event handler for the Event type you need and that is it - no need for Pulumi code.
Example
Let’s say you created a part of code which sends out an SQS message, and you want to have a Lambda which handles that message.
Good example would be if you want to run some calculation, asynchronously, from our GraphQL Lambda. You would insert an SQS Message and in turn it would trigger a Lambda which you have defined. That lambda should have code similar to this:
Multiple Event Handlers in a Single Handler
Multiple Event Handlers of a Same Type in a Single Handler
You can define multiple event handlers, for example DynamoDB event handlers, in a single handler.
They will be executed in reverse order, meaning that the last one defined will be executed first.
There are few things to note:
- You should call
await next();in your event handler, otherwise the next event handler will not be executed. - You can return anything you want to from your event handler, just note that the next event handler will receive that value as a result if the
await next();call.
Event Handler Response
When handling an event, you can either return the reply object or something else, what ever you like.
Basically, when you return the reply, a standard APIGatewayProxyResult is created out of the data, headers and cookies you sent.
When you return anything else other than the
reply, it is returned as the result of the handler, and the Lambda itself.
For example, you can send plain text or object to get the response of the Lambda without the need to parse the APIGatewayProxyResult.