Lua Filter

In this example, we show how a Lua filter can be used with the Envoy proxy. The Envoy proxy configuration includes a Lua filter that contains two functions namely envoy_on_request(request_handle) and envoy_on_response(response_handle) as documented here.

Running the Sandboxes

The following documentation runs through the setup of Envoy described above.

Step 1: Install Docker

Ensure that you have a recent versions of docker and docker-compose installed.

A simple way to achieve this is via the Docker Desktop.

Step 2: Clone the Envoy repo

If you have not cloned the Envoy repo, clone it with:

SSH

HTTPS

  1. git clone git@github.com:envoyproxy/envoy
  1. git clone https://github.com/envoyproxy/envoy.git

Step 3: Build the sandbox

  1. $ pwd
  2. envoy/examples/lua
  3. $ docker-compose pull
  4. $ docker-compose up --build -d
  5. $ docker-compose ps
  6. Name Command State Ports
  7. --------------------------------------------------------------------------------------------------------------------
  8. lua_proxy_1 /docker-entrypoint.sh /bin Up 10000/tcp, 0.0.0.0:8000->8000/tcp, 0.0.0.0:8001->8001/tcp
  9. lua_web_service_1 node ./index.js Up 0.0.0.0:8080->80/tcp

Step 4: Send a request to the service

The output from the curl command below should include the headers foo.

Terminal 1

  1. $ curl -v localhost:8000
  2. Trying ::1...
  3. * TCP_NODELAY set
  4. * Connected to localhost (::1) port 8000 (#0)
  5. > GET / HTTP/1.1
  6. > Host: localhost:8000
  7. > User-Agent: curl/7.64.1
  8. > Accept: */*
  9. >
  10. < HTTP/1.1 200 OK
  11. < x-powered-by: Express
  12. < content-type: application/json; charset=utf-8
  13. < content-length: 544
  14. < etag: W/"220-IhsqVTh4HjcpuJQ3C+rEL1Cw1jA"
  15. < date: Thu, 31 Oct 2019 03:13:24 GMT
  16. < x-envoy-upstream-service-time: 1
  17. < response-body-size: 544 <-- This is added to the response header by our Lua script. --<
  18. < server: envoy
  19. <
  20. {
  21. "path": "/",
  22. "headers": {
  23. "host": "localhost:8000",
  24. "user-agent": "curl/7.64.1",
  25. "accept": "*/*",
  26. "x-forwarded-proto": "http",
  27. "x-request-id": "a78fcce7-2d67-4eeb-890a-73eebb942a17",
  28. "foo": "bar", <-- This is added to the request header by our Lua script. --<
  29. "x-envoy-expected-rq-timeout-ms": "15000",
  30. "content-length": "0"
  31. },
  32. "method": "GET",
  33. "body": "",
  34. "fresh": false,
  35. "hostname": "localhost",
  36. "ip": "::ffff:172.20.0.2",
  37. "ips": [],
  38. "protocol": "http",
  39. "query": {},
  40. "subdomains": [],
  41. "xhr": false,
  42. "os": {
  43. "hostname": "7ca39ead805a"
  44. }
  45. * Connection #0 to host localhost left intact
  46. }* Closing connection 0