Create an Event Hook that can run custom code with Kong Gateway
The lambda
Event Hook handler can be used to pass custom Lua code. You can then configure the Event Hook to execute that code on an event.
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
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.
Create a lambda Event Hook
A lambda
Event Hook is an Event Hook that utilizes the lambda
handler to pass custom code to an Event Hook. Depending on the source and individual event, that code can execute during various stages of the lifecycle of an event. In this guide, you will create an lambda
Event Hook with custom code that logs an error with a specific message every time you create a Consumer.
Create a Lua script to load into the lambda Event Hook.
return function (data, event, source, pid)
local user = data.entity.username
error("Event Hook on consumer " .. user .. "")
end
Create a lambda
Event Hook on the consumers
event, with the crud
source by creating a POST
request to the Admin API and passing the code in the request body as an array of strings.
curl -i -X POST http://localhost:8001/event-hooks/ \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--data '
{
"source": "crud",
"event": "consumers",
"handler": "lambda",
"config": {
"functions": [
"return function (data, event, source, pid) local user = data.entity.username error(\"Event Hook on consumer \" .. user .. \"\") end"
]
}
}
'
Validate the webhook
Important: Before you can use Event Hooks for the first time, Kong Gateway needs to be reloaded.
Using the Admin API 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"
}
'
Review the logs at /usr/local/kong/logs/error.log
for an update about the creation of this Consumer. The log will look similar to this:
2024/12/16 21:52:54 [error] 114#0: *153047 [kong] event_hooks.lua:190 [string "return function (data, event, source, pid)..."]:3: Event Hook on consumer my-consumer, context: ngx.timer, client: 172.19.0.1, server: 0.0.0.0:8001
In the error logs, you will see the Event Hook and the error log that resulted from error("Event Hook on consumer " .. user .. "")
.
Cleanup
Destroy the Kong Gateway container
curl -Ls https://get.konghq.com/quickstart | bash -s -- -d