[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
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
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.
let tear down what is in this Serverless YAML:
service
will fill with the name of your service.
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
.
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.
The kind of provider that we use for our Serverless app, for now we use aws
and nodejs
as run time.
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
.
After running the app, try to find TopicArn
it is the topic that our application will consume.
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.
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.