Push Event Hook information to Slack with Kong Gateway

Uses: Kong Gateway
TL;DR

With an application URL from Slack, you can configure an Event Hook using the webhook-custom handler that can POST event information to Slack.

Prerequisites

This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

To create an Event Hook that pushes information to Slack, you will need to configure some options in Slack.

  1. Create an application in Slack
  2. From the application select Incoming Webhooks and activate incoming webhooks.
  3. Add a new webhook and select a Slack channel.
  4. Copy the Webhook URL from the Slack application dashboard and set it as an environment variable: export SLACK_WEBHOOK_URL='WEBHOOK URL

cURL is used to send requests to Kong Gateway. curl is pre-installed on most systems.

Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded. To do this, run kong-reload. You can verify that this worked by issuing a GET request to the /event-hooks/ endpoint.

Configure an Event Hook using the webhook-custom handler

Using the webhook-custom handler, you can configure an Event Hook that listens for events on a source. The webhook-custom handler offers a template that you can configure to create a custom webhook. In this tutorial, we will configure an Event Hook that issues a POST request when a crud event happens on the Consumer entity. That POST request will be made to a Slack webhook application containing a custom message describing the event.

curl -i -X POST http://localhost:8001/event-hooks/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "source": "crud",
      "event": "consumers",
      "handler": "webhook-custom",
      "on_change": true,
      "config": {
        "method": "POST",
        "url": "'$SLACK_WEBHOOK_URL'",
        "headers": {
          "Content-type": "application/json"
        },
        "payload": {
          "text": "new consumer added"
        }
      }
    }
    '

Posting this will result in a 200 response. The config body in the Event Hook contains information about the webhook that was created:

  • "method": "POST": The method we are using in the webhook.
  • "url": "$SLACK_WEBHOOK_URL": The URL of the webhook. In this case, we are using the Slack URl that we created and set as an environment variable.
  • "payload": What this webhook will POST.

Validate the webhook

Important: Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded.

Use the Admin API to create a new Consumer:

curl -i -X POST http://localhost:8001/consumers/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "username": "my-consumer"
    }
    '

Slack will post a message to the channel informing you that a Consumer was added.

Cleanup

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!