Skip to content

How to use Lua correctly in krakend CE #1021

@KnowledgeSeekers

Description

@KnowledgeSeekers

Environment info:

  • KrakenD version: 2.4
  • System info: docker
  • Hardware specs:
  • Backend technology: Java
  • Additional environment information:

Describe what are you trying to do:
I am working on a POC using krakend and I need some basic string transfomations like truncating some caracters from a string, I use lua script and all string functions are not working.
my function :

  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

result :

{"@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

Your configuration file:

{
  "$schema": "https://www.krakend.io/schema/krakend.json",
  "version": 3,
  "name": "KrakenD - API Gateway",
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "port": 8080,
  "debug_endpoint": true,
  "extra_config": {
    "telemetry/logging": {
      "level": "DEBUG",
      "stdout": true,
      "format": "logstash",
      "prefix": "[KRAKEND]"
    }
  },
  "endpoints": [
    {
      "endpoint": "/v1/getDataMobileBackend/{id_csu}",
      "method": "GET",
      "output_encoding": "json",
      "timeout": "10s",
      "input_headers": [
        "Authorization",
        "User-Agent",
        "X-Request-ID"
      ],
      "backend": [
        {
          "url_pattern": "/api/Url/{input}",
          "encoding": "json",
          "sd": "static",
          "method": "GET",
          "host": [
            "http://mybackend"
          ],
          "disable_host_sanitize": false,
          "timeout": "5s",
          "group": "",
          "allow": [
            "provider",
            "imei",
            "msisdn"
          ],
          "mapping": {},
          "target": "data",
          "extra_config": {
            "modifier/lua-backend": {
              "sources": ["./lua/log.lua"],
              "pre": "",
              "post": "local r = response.load(); log_backend_response(r)"
            }
          }
        }

Configuration check output:
Result of krakend check -dtc krakend.json --lint command

 podman exec -it c85ca3a1021d sh
/etc/krakend # krakend check -dtc krakend.json --lint
Parsing configuration file: krakend.json
Global settings
        Name: KrakenD - API Gateway
        Port: 8080
1 global component configuration(s):
- telemetry/logging
1 API endpoint(s):
        - GET /v1/getDataMobileBackend/:id_csu
        Timeout: 2s
        Connecting to 2 backend(s):
                [+] GET /api/url1/{{.Id_csu}}
                Timeout: 2s
                Hosts: [http://10.144.0.5]
                1 backend component configuration(s):
                - modifier/lua-backend

                [+] GET /api/url2/{{.Id_csu}}?inputServiceOnly=true
                Timeout: 2s
                Hosts: [http://10.144.0.5]
                1 backend component configuration(s):
                - modifier/lua-backend

0 async agent(s):
Syntax OK!

Commands used:
How did you start the software?

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"]

Commands:
podman build -t krakend-lua-image .
podman run --rm -p 8080:8080 krakend-lua-image

Logs:
{"@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 #1: attempt to index a non-table objec

Are there any spcific parameters that I should add to use string and other modules ? or should I use a go plugin ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions