Dynamic Routing in Serverless Microservice with Vert.x Event Bus
GOAL:
Create a reactive, message-driven, asynchronous User Microservice with GET
, POST
, DELETE
, PUT
CRUD operations in a single AWS Lambda Function using the Serverless Framework
Serverless stack definition
:
SOLUTION:
Use Vert.x's Event Bus to handle dynamic routing to event handlers based on HTTP method and resource path from the API input.
Lambda Handler
:
CODE REVIEW
Lines 14-19
initializes the Vert.x instance. AWS Lambda will hold on to this instance for the life of the container/JVM. It is reused in subsequent requests.
Line 17
registers the User Service handlers
Line 22
defines the main handler method that is called when the Lambda function is invoked.
Line 27
sends the Lambda function input to the (dynamic) address where handlers are waiting to respond.
Lines 44-66
defines the specific handlers and binds them to the appropriate channels (http method + resource path)
Serverless + Vert.x = BLISS