Serverless Consulting

Send Slack Notifications via AWS lambda

Using Incoming Webhooks

Send Slack Notifications via AWS lambda

By
Dharmendra Singh
March 18, 2022

Introduction

In this article, we'll go over how to use Incoming Webhooks to send a slack message from AWS Lambda.

The entire PoC (proof-of-concept) uses IaC written with Serverless Framework. You can access this article's FREE template for download HERE

What is an Incoming Webhook?

Incoming Webhooks are a simple way to post messages from external sources into Slack. They make use of normal HTTP requests with a JSON payload, which includes the message and some options. [1]

Above is an architecture diagram showing an API gateway calling lambda function and lambda function sending notification to slack.

Adding the Incoming WebHooks Plugin into Slack.

Go to the Slack App Directory and search: Incoming Webhooks.

Click on Incoming WebHooks and it will display like this:

Next, click on: "Add to Slack"

After adding, you'll be redirected to the settings page for your new app (if you are using an existing app, just load its settings via your app's management dashboard).

From here select the Incoming Webhooks feature, and click the Activate Incoming Webhooks toggle to switch it on. [2]

After the above step, it will generate a curl command and an option to add the newly created Webhook to a slack workspace.

Next, click on the button "Add New Webhook to Workspace "and pick a channel that the app will post to, and then click to Allow your app.

Next, you will be sent back to your app settings, and you should now see a new entry under the Webhook URLs for Your Workspace section, with a Webhook URL that'll look something like this [3]:

Clone Template and Deployment

Now, get the SLS template from here.

Then, update the Webhook URL and Channel Name in the lambda environment variables like this:

Next, run this in your terminal:

  
npm install && sls deploy
  

Once the deployment process is completed, we will receive the API endpoint.

Now, using Postman (or any other tool) call your API endpoint; you should see a notification in your Slack channel that looks like this:

Conclusion

By utilizing Incoming Webhooks, we have seen how to send notifications to our slack channel via the lambda function. Since Slack is one of the most extensively used communication tools,  we can now send alerts to Slack channels, which helps teams to communicate more effectively.

If you haven't already, visit this page and download this article's template for free HERE

Sources

[1] https://api.slack.com/messaging/webhooks?utm_medium=ppc&utm_source=google&utm_campaign=ppc_google_in_en_dsa_nonbrand&utm_term=&campaign=7013a000001ru2vAAA&gclid=CjwKCAiA3L6PBhBvEiwAINlJ9MYnfrLSSs7evQNei0m2LBTGaRIW9-PQifYI4a0prHX0H56X3J_2CBoCECwQAvD_BwE&gclsrc=aw.ds

[2] https://api.slack.com/messaging/webhooks

[3] https://medium.com/datareply/integrating-slack-alerts-in-airflow-c9dcd155105