Making alert messages event-based using Google Cloud Pub/Sub

Photo by Volodymyr Hryshchenko on Unsplash

Have you ever been in that situation where you have to send email or Slack messages for alerts from your microservices and ended up putting the same webhook URL or secrets and replicating the code in pretty much every service? Well, there’s an easier way to manage it, by using pub/sub events and cloud functions — async events like they are supposed to be!

In this article, I’ll be putting forward a simple setup using Google Cloud Pub/Sub and Cloud functions to send Slack messages, used by NodeJS microservices, but it can be replicated using any queue and serverless platform.

How it works

Simple architecture

Services publish messages on the slack-notification pub/sub topic -> Notification sender cloud function having the topic as the trigger processes the message -> Sends the message to Slack

How to setup

Assuming you already have your microservices on Google Cloud, we can start by creating a topic in Pub/Sub. I’ll call it slack-notification and won’t be attaching any default subscription for it.

Pub/Sub topic creation modal

Once the topic is created, we can create the cloud function which gets triggered from the above topic and processes each of the messages. Head over to the Cloud Functions page and create a function.

Creating cloud function — Step 1

We’ll create a function with the 1st gen. environment as the newer 2nd gen. does not support pub/sub trigger as of writing this article. Select the trigger type as pub/sub and select the topic we created earlier. You can also enable Retry on failure here if it’s necessary.

Creating cloud function — Step 2

Let’s move on to the runtime and other settings to setup the function. You can configure the parameters similar to the above picture. Two points to note:

  • Make sure you choose the right service account for the function. This depends on your project setup, for providing it with the right accesses and permissions.
  • Add your Slack webhook URL or any other required environment variables in the runtime environment variables. These will be used by the code in the function to deliver the notifications.
Creating cloud function — Step 3

Our configuration of the function is done now, let’s add the code to be run in the function that would send the notification to Slack. I’ve used NodeJS here but you can use any language the platform supports.

I’ve used Axios for making the call to the Slack webhook here. We can use any NPM package but have to make sure we include it in the package.json in the function. Also, another thing to note here is that the data from the pub/sub message received here is Base64 encoded and we would need to decode it to get the contents. Once we create the function, it would take a few minutes to deploy and we are done with the setup!

Testing the setup — Step 1

We can test the whole setup by using the GCP UI itself, just go to the slack-notification pub/sub topic and click on MESSAGES > PUBLISH MESSAGE to reveal the option for sending manual messages.

Testing the setup — Step 2

Just enter the message body in the correct JSON format and hit PUBLISH and if the setup is proper, you should receive a Slack message to the provided channel!

To use this across your services, you could use the SDK provided by Google, for example, the @google-cloud/pubsub NPM package for NodeJS or make a direct HTTP call as defined in the docs. Make sure that the service publishing the messages is running under the same project in GCP and has access to the pub/sub topic.

Useful Resources:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahesh H P

Mahesh H P

44 Followers

Full stack developer | Occasional Blogger | Love ☕ | Opinions here are my own | https://hpmahesh.me