I am working on a POC using krakend and I am having a hard time using lua. I need basic transformation but all functions in string module don't seem to work. what am I doing wrong ?
Dockerfile :
FROM krakend:2.4
RUN mkdir -p /etc/krakend/lua
COPY krakend-court.json /etc/krakend/krakend.json
COPY lua/ /etc/krakend/lua/
RUN chmod -R 755 /etc/krakend/lua
EXPOSE 8080
ENV KRAKEND_PORT=8080
ENV FC_ENABLE=1
ENV FC_PARTIALS=/etc/krakend/partials
CMD ["krakend", "run", "-d", "-c", "/etc/krakend/krakend.json"]
lua script :
function log_backend_response(response)
safe_log("---------- REPONSE BACKEND ----------", "[BACKEND-IN]")
local responseData = response:data()
local imei = responseData:get('imei')
safe_log("IMEI: " .. imei, "INFO")
local imei_str = tostring(imei)
local result = imei_str:sub(1, 8)
end
krakend.json :
"extra_config": {
"modifier/lua-backend": {
"sources": ["./lua/log.lua"],
"pre": "",
"post": "local r = response.load(); log_backend_response(r)"
}
}
error :
{"@timestamp":"2025-07-21T15:56:09.706+00:00", "@version": 1, "level": "ERROR", "message": "[ENDPOINT: /v1/myEndPoint/input] Error #0: attempt to index a non-table objec", "module": "KRAKEND"}
Error #01: attempt to index a non-table objec
I can see in lua examples in github that they are using the function sub. Should I use a go plugin ? Thank you
string.sub()
is a Lua standard library function. According to the KrakenD documentation:
... if you need to use the standard library, you will need to add in the configuration the flag
allow_open_libs
.
The description for allow_open_libs
says:
If you need to import Lua libraries (e.g, the I/O, String, etc.), then you must set this flag to true.
I haven't used KrakenD, but from the configuration docs it looks like you need to modify your krakend.json
file:
"extra_config": {
"modifier/lua-backend": {
"sources": ["./lua/log.lua"],
"pre": "",
"post": "local r = response.load(); log_backend_response(r)"
"allow_open_libs": true,
}
}