Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.40.x.
Can I use this?

This feature is available since Webiny v5.39.0.

What you’ll learn
  • How routing works.
  • How to add new events.
  • How to add new routes.

About
anchor

In the 5.31.0 version of Webiny we have refactored our handler package (@webiny/handler) to use fastifyexternal link. Fastify has a quite extensive documentationexternal link, 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 routesexternal link and event handling
  • consistent requestexternal link and replyexternal link (formerly response in our system) methods throughout the system
  • a lot of request lifecycle eventsexternal link 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
anchor

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
anchor

The underlying event handlers, which the main createHandler chooses depending on the event type, actually initialize the Webiny system. They will initialize fastifyexternal link, 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)
anchor

This handler uses @fastify/aws-lambdaexternal link in the background to handle API Gateway events. It transforms APIGatewayEventexternal link into the request which fastify understands, and it is then used throughout the system.

For this handler to work it requires at least one RoutePluginexternal link to be initialized. It always returns LambdaResponseexternal link 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
anchor

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 APIGatewayProxyResultexternal link or anything else that is directly sent from the EventPluginexternal link.

S3 Event
anchor

EventBridge Event
anchor

SQS Event
anchor

SNS Event
anchor

DynamoDB Event
anchor

You can define as much routes and events as you like in a single `createHandler` method.

Adding New Routes
anchor

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:

apps/api/graphql/src/index.ts
Security checks will be done as on the /graphql endpoint. If you need customizations, feel free to add them.
Routes can be added only when initializing the handler.

Adding Event Handler
anchor

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
anchor

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
anchor

Multiple Event Handlers of a Same Type in a Single Handler
anchor

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
anchor

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 APIGatewayProxyResultexternal link 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.