DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on

# Lamda , eventbridge, sqs

Lamda , eventbridge, sqs


Step 1: System Created in organization_webapi

  • When a system is created (e.g., via an API call), the service layer in organization_webapi handles the logic.
  • After saving the system to the database, it publishes an event to EventBridge.

Relevant code:

// apps/organization_webapi/app/service/dispatchEvents/index.js

async function postSystem(_id) {
  const Model = await this.Model;
  const data = await Model.System.findOne({ _id }).populate('address');
  const evtObj = new EventBridge(this.expressReq);
  return evtObj.putEvents(getEventBridgeEvent('org.post.system', data));
}

Enter fullscreen mode Exit fullscreen mode
  • This function fetches the new system, creates an EventBridge event (org.post.system), and publishes it.

Step 2: EventBridge Receives the Event

  • EventBridge is an AWS service that acts as an event bus.
  • The event (org.post.system) is sent to EventBridge.

Configuration for EventBridge is typically found in:

  • apps/organization_webapi/yml/Resource/Events.yml
  • apps/organization_webapi/serverless/OrganizationEvents.js

Step 3: EventBridge Forwards Event to SQS

  • There is a rule in EventBridge that matches the event (org.post.system) and forwards it to an SQS queue.
  • SQS is a queue that temporarily stores the event.

Configuration for this is in:

  • apps/organization_webapi/yml/Resource/Events.yml
  • apps/organization_webapi/yml/Resource/Resource.yml

Step 4: Lambda is Triggered by SQS

  • A Lambda function is set up to listen to the SQS queue.
  • When a new event arrives, Lambda is triggered and processes the event.

Lambda handler example (in user_webapi):

// apps/user_webapi/serverless/UserEvents.js

Router.source('org.post.system', async function (event, body) {
  const systemResult = await new SystemService(this.expressReq).insertSystem(body);
  return await new UserService(this.expressReq).attachedSystemToFirstUser(systemResult._id);
});
exports.handler = async event => {
  return Router.listen(event);
};

Enter fullscreen mode Exit fullscreen mode
  • This code listens for the org.post.system event and creates the system in the user service.

Step 5: user_webapi Creates the System

  • The Lambda function (above) calls the user service logic to create the system in the user database.

Summary Table

Step Service/Component File/Folder Example What Happens
1 organization_webapi app/service/dispatchEvents/index.js System is created, event is published to EventBridge
2 EventBridge yml/Resource/Events.yml EventBridge receives the event
3 SQS yml/Resource/Resource.yml EventBridge forwards event to SQS queue
4 Lambda serverless/UserEvents.js Lambda is triggered by SQS, processes event
5 user_webapi app/service/system.js System is created in user database

In Simple Terms

  1. organization_webapi creates a system and tells EventBridge.
  2. EventBridge forwards the event to an SQS queue.
  3. Lambda picks up the event from SQS and tells user_webapi to create the same system.
  4. user_webapi creates the system for the user.

If you want to see the actual YAML/serverless config or more code for any step, let me know!

Top comments (0)