[DX] Creating Event Driven Service Using Serverless + SNS + NestJS

[DX] Creating Event Driven Service Using Serverless + SNS + NestJS

TLDR;

This article will demonstrate how to using sns offline plugin to have better experience while developing Serverless service using NestJS framework.

Intro

At first, for creating serverless service using NestJS, you can read the documentation Serverless NestJS doc . On this article we won't show you how to create a serverless service from scratch, we will more focus on how we setup our workspace to have better development experience on it. This issue pop up on my office when we want to create NestJS Serverless service, we didn't test in our local and straight into our staging stage, which is more like do a gamble on my POV, we can be more confident on something that we build if we had tested irt on our local environment.

Setup

Screen Shot 2022-04-03 at 16.09.15.png

On this project, we just gonna have 1 module, that have 2 endpoint

  • [GET] /serverless/check-health: to check if its running well or not .
  • [POST] /serverelss: to post a publish an event.

You can clone the project from this repository sls-sns-repo .

Service

ckckc.png

On line 4 - 5 we initiate our SNS Service. the endpoint will be explained on other section, bear with me.

as the methods declare below the constructor we just make 2 service, one as publisher and the other one as consumer / subscriber.

Serverless YAML

Lets jump into the Serverless YAML, here's the part when we are setting up our local workspace to have ability running Serverless + SNS service locally.

sns_offline.png

let tear down what is in this Serverless YAML:

service_name.png

service will fill with the name of your service.

plugins.png

Here's the plugins that we use to perform the offline SNS service. Serverless-offline, adding this plugin so our application, can run Serverless on our local machine. Serverless-offline-sns is the plugin we need so our Serverless app have capability of AWS SNS.

custom.png

On this custom property, we need to set this Serverless-offline-sns props. to 2 property that you need to be concerned are host and port. these variables are that we use on our service as we mention before.

provider.png

The kind of provider that we use for our Serverless app, for now we use aws and nodejs as run time.

function.png

here's the list of functions that available for our application. main function is monolith REST service that we declare so our REST can be accessed through this function. The incoming function will be used as handler for sns event.

Demo

Lets start the application using sls offlline.

Screen Shot 2022-04-03 at 23.02.56.png

After running the app, try to find TopicArn it is the topic that our application will consume.

publisher.png

like on this service, on publishEvent method, we add TopicArn that we found on the terminal so we can publish to the event and will be consumed by the incoming function.

In Action

Try to make a request to our POST endpoint, and see on the terminal.

Screen Shot 2022-04-03 at 23.09.15.png

As we can see our incoming function was triggered and our sns event that we publish was consumed successfully.

Conclusion

Using serverless-offline-sns will help us to have better experience and simulate our sns event properly, so we will have more confidence regarding logic business that we build for our application.

resources

Did you find this article valuable?

Support Muhammad Hafizh Abdillah AR by becoming a sponsor. Any amount is appreciated!