Push Event Hook information to Slack with Kong Gateway
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
Kong Gateway running
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.
-
Export your license to an environment variable:
export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
-
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
A Slack webhook application
To create an Event Hook that pushes information to Slack, you will need to configure some options in Slack.
- Create an application in Slack
- From the application select Incoming Webhooks and activate incoming webhooks.
- Add a new webhook and select a Slack channel.
- Copy the Webhook URL from the Slack application dashboard and set it as an environment variable:
export SLACK_WEBHOOK_URL='WEBHOOK URL
Reload Kong Gateway
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 willPOST
.
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
Destroy the Kong Gateway container
curl -Ls https://get.konghq.com/quickstart | bash -s -- -d