Create a Proxy-Wasm filter

Your first Proxy-Wasm filter

Selecting a language

In order to be viable for filter development, your programming language of choice must meet the following criteria:

  1. Supports WebAssembly as a build target
  2. Has a Proxy-Wasm SDK

The following language SDKs are currently well-tested in Kong Gateway, making them a good choice for your first filter:

Other languages and/or Proxy-Wasm SDKs are theoretically supported but have not yet been fully tested.

Kong has made Go and Rust Proxy-Wasm filter templates available. They can be used to start writing your own filters, or to build as they are and get your first hello world-type filter:

Deploying the filter with Kong

After writing and building the filter, the next steps are to configure Kong to enable Wasm support and add the filter, then associate the filter to a route or a service. You will then be able to start Kong and issue requests that go through the filter.

Configuration

WebAssembly support must be enabled in Kong Gateway:

  1. $ export KONG_WASM=on

Additionally, the wasm_filters_path parameter must be configured in order for Kong Gateway to load the filter at runtime. This can be any folder containing .wasm files.

During local development, when a short feedback loop is desired, you can set this parameter to your build toolchain’s output directory (wherever the compiled <filter-name>.wasm file is produced):

  1. $ export KONG_WASM_FILTERS_PATH=/path/to/my_filter/build

Now, the next step is to create a Kong Filter Chain entity using the filter created in the previous step and associate it with a Kong route or service. This will be done by creating a declarative yaml config file, e.g. kong.yml:

  1. ---
  2. _format_version: '3.0'
  3. services:
  4. - name: my-wasm-service
  5. url: http://mockbin.org
  6. routes:
  7. - name: my-wasm-route
  8. paths:
  9. - /
  10. filter_chains:
  11. - name: my-wasm-demo
  12. filters:
  13. - name: my_filter
  14. config: >-
  15. {
  16. "my_greeting": "Hello from Kong using WebAssembly!"
  17. }
  1. $ export KONG_DATABASE=off
  2. $ export KONG_DECLARATIVE_CONFIG="$PWD/kong.yml"

Start Kong

Now Kong can be started:

  1. $ export KONG_PREFIX="$PWD/wasm-servroot"
  2. $ kong prepare
  3. $ kong start

And the Proxy-Wasm filter is ready to be used:

  1. $ http --headers http://127.0.0.1:8000/
  2. HTTP/1.1 200 OK
  3. Connection: keep-alive
  4. Content-Encoding: gzip
  5. Content-Type: text/html; charset=utf-8
  6. Date: Thu, 03 Aug 2023 19:28:27 GMT
  7. Vary: Accept-Encoding
  8. Via: kong/3.4.0
  9. X-Greeting: Hello from Kong using WebAssembly!
  10. X-Kong-Proxy-Latency: 1
  11. X-Kong-Upstream-Latency: 342

Further Reading


Previous (un)Installing your plugin